-
Notifications
You must be signed in to change notification settings - Fork 90
/
ch13a.html
66 lines (41 loc) · 2.9 KB
/
ch13a.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Study guide for the Oracle Certified Professional, Java SE 8 Programmer Exam ">
<title>Java 8 Programmer II Study Guide: Exam 1Z0-809</title>
<link href="css/code.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="js/common-sections.js"></script>
</head>
<body>
<div class="header">
<div class="title-container">
<div class="chapter-title">
<h1><i class="chapter">Chapter THIRTEEN</i><br />
Iterating and Filtering Collections</h1>
<p><br /></p>
<h3 style="text-align: center;"><i>Exam Objectives</i></h3>
<p style="text-align: center;"><i>Collections Streams and Filters.<br /></i><i>Iterate using forEach methods of Streams and List.<br /></i><i>Filter a collection by using lambda expressions.</i></p>
</div>
</div>
</div>
<div class="container">
<div class="column">
<h2>Answers</h2>
<p><b>1. The correct answer is D.</b><br />
<code>Stream.of(T t)</code> creates a stream of a single element, in this case of type <code>List<Integer></code>. So <code>i</code> in the lambda expression has this type. Therefore, <code>i - 1</code> is an invalid expression that generates a compiler error. To create a stream from this list, you have to use <code>l.stream()</code>.</p>
<p><br /></p>
<p><b>2. The correct answer is C.</b><br /> Option A is false. <code>filter</code> is an intermediate operation.<br /> Option B is false. <code>filter</code> is a stateless operation since the predicate of the filter is applied to each element of the stream independently.<br /> Option C is true. <code>Stream.forEach</code> takes an implementation of the <code>Consumer</code> functional interface as an argument.<br /> Option D is false. <code>forEach</code> is a terminal operation, so you can't chain two <code>forEach</code> operations.</p>
<p><br /></p>
<p><b>3. The correct answer is B.</b><br /> The filter operation generates a stream with only even values (<code>2</code>, <code>4</code>, <code>6</code>). A second filter generates a stream with values greater than three (<code>4</code>, <code>6</code>), that are finally printed.</p>
<p><br /></p>
<p><b>4. The correct answer is C.</b><br /> The filter returns an empty stream (since none of the elements match the condition), so nothing else is filtered or printed.</p>
<p><br /></p>
</div>
</div>
<footer></footer>
</body>
</html>