-
Notifications
You must be signed in to change notification settings - Fork 90
/
ch31.html
380 lines (281 loc) · 23 KB
/
ch31.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<!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" />
<link href="https://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
<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="nav"></div>
<div class="header">
<div class="title-container">
<div class="chapter-title">
<h1><i class="chapter">Appendix A</i><br />
From Java 6/7 to Java 8</h1>
<p><br /></p>
<h3 style="text-align: center;"><i>Exam Objectives</i></h3>
<p style="text-align: center;"><i>Develop code that uses Java SE 8 collection improvements, including Collection.removeIf(), List.replaceAll(), Map.computeIfAbsent(), and Map.computeIfPresent() methods.</i><br /><i>Develop code that uses String objects in the switch statement, binary literals, and numeric literals, including underscores in literals.</i><br /><i>Use Lock, ReadWriteLock, and ReentrantLock classes in the java.util.concurrent.locks.</i><br /><i>Format dates, numbers, and currency values for localization with the NumberFormat and DateFormat classes, including number and date format patterns.</i><br /><i>Recursively access a directory tree by using the DirectoryStream and FileVisitor interfaces</i><br /><i>Find a file by using the PathMatcher interface.</i><br /><i>Observe the changes in a directory by using the WatchService interface.</i></p>
</div>
</div>
</div>
<div class="container">
<div class="column">
<h2>New Methods on Collections</h2>
<p>Java 8 brings some new helper methods for <code>Collection</code>s to write less verbose code.</p>
<p>The first one is <code>forEach()</code> (we talked about it earlier):</p>
<p><code class="java hljs"><span class="hljs-function"><span class="hljs-keyword">default</span> <span class="hljs-keyword">void</span> <span class="hljs-title">forEach</span><span class="hljs-params">(Consumer<? <span class="hljs-keyword">super</span> T> action)</span></span></code></p>
<p>That calls the <code>accept()</code> method on each element of the collection. So instead of having something like:</p>
<p><code class="java hljs"><span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < list.size(); i++) {<br />
System.out.println(list.get(i));<br />
}</code></p>
<p>Or:</p>
<p><code class="java hljs"><span class="hljs-keyword">for</span>(String s : list) {<br />
System.out.println(s);<br />
}</code></p>
<p>Now we can have it as (without creating a stream):</p>
<p><code class="java hljs">list.forEach(System.out::println);</code></p>
<p>Another one is <code>removeif()</code>:</p>
<p><code class="java hljs"><span class="hljs-function"><span class="hljs-keyword">default</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">removeIf</span><span class="hljs-params">(Predicate<? <span class="hljs-keyword">super</span> E> filter)</span></span></code></p>
<p>That iterates over the elements of the collection and removes an element if it matches the given predicate (if the implementation of the <code>Collection</code> doesn't support removal, an <code>UnsupportedOperationException</code> is thrown). So instead of having something like:</p>
<p><code class="java hljs">Iterator<String> it = list.iterator();<br />
<span class="hljs-keyword">while</span> (it.hasNext()) {<br />
String s = it.next();<br />
<span class="hljs-keyword"> if</span>(s != <span class="hljs-keyword">null</span> && s.length() < <span class="hljs-number">3</span>) {<br />
it.remove();<br />
}<br />
}</code></p>
<p>Now we can have just:</p>
<p><code class="java hljs">list.removeIf( s -> s != <span class="hljs-keyword">null</span> && s.length() < <span class="hljs-number">3</span> );</code></p>
<p>We also have <code>replaceAll()</code>:</p>
<p><code class="java hljs"><span class="hljs-function"><span class="hljs-keyword">default</span> <span class="hljs-keyword">void</span> <span class="hljs-title">replaceAll</span><span class="hljs-params">(UnaryOperator<E> operator)</span></span></code></p>
<p>That replaces each element of this list with the result of applying the operator to the element. So instead of having something like:</p>
<p><code class="java hljs">Iterator<String> it = list.iterator();<br />
<span class="hljs-keyword">while</span> (it.hasNext()) {<br />
String s = it.next();<br />
it.set(s.toUpperCase());<br />
}</code></p>
<p>Now we just have:</p>
<p><code class="java hljs">list.replaceAll(s -> s.toUpperCase());</code></p>
<h2>New Methods on Maps</h2>
<p>Java 8 also brings new helper methods to <code>Map</code>s. There's two interesting methods in particular.</p>
<p>One is <code>computeIfAbsent()</code>:</p>
<p><code class="java hljs"><span class="hljs-function"><span class="hljs-keyword">default</span> V <span class="hljs-title">computeIfAbsent</span><span class="hljs-params">(<br />
K key,<br />
Function<? <span class="hljs-keyword">super</span> K,? extends V> mappingFunction)</span></span></code></p>
<p>This method calculates the value of the given key, and adds it to the map only if:</p>
<ul>
<li>The key is not already in the map</li>
<li>The key has a <code>null</code> value associated</li>
<li>If the calculated value is different than <code>null</code></li>
</ul>
<p>For example:</p>
<p><code class="java hljs">Map<String, String> letters = <span class="hljs-keyword">new</span> HashMap<>();<br />
letters.put(<span class="hljs-string">"a"</span>, <span class="hljs-string">"a"</span>);<br />
letters.put(<span class="hljs-string">"b"</span>, <span class="hljs-keyword">null</span>);<br />
<br />
Function<String, String> func = k -> {<br />
<span class="hljs-keyword"> if</span>(k.startsWith(<span class="hljs-string">"d"</span>)) <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;<br />
<span class="hljs-keyword"> return</span> k.toUpperCase();<br />
};<br />
<span class="hljs-comment"><br />
// Won't update,already in map</span><br />
letters.computeIfAbsent(<span class="hljs-string">"a"</span>, func);<br />
<span class="hljs-comment">// Will update<br /></span> letters.computeIfAbsent(<span class="hljs-string">"b"</span>, func);<br />
<span class="hljs-comment">// Will add</span><br />
letters.computeIfAbsent(<span class="hljs-string">"c"</span>, func);<br />
<span class="hljs-comment">// Won't add</span><br />
letters.computeIfAbsent(<span class="hljs-string">"d"</span>, func);<br />
<span class="hljs-comment">// Yes, there's also a forEach in Maps</span><br />
letters.forEach( (key, value) -><br />
System.out.format(<span class="hljs-string">"%s-%s;"</span>, key, value)<br />
);</code></p>
<p>The output:</p>
<p><code class="java hljs">a-a;b-B;c-C;</code></p>
<p>The other method is <code>computeIfPresent()</code>:</p>
<p><code class="java hljs"><span class="hljs-function"><span class="hljs-keyword">default</span> V <span class="hljs-title">computeIfPresent</span><span class="hljs-params">(<br />
K key,<br />
BiFunction<? <span class="hljs-keyword">super</span> K,? <span class="hljs-keyword">super</span> V,? extends V> remappingFunction)</span></span></code></p>
<p>This method updates the value of a given key only if:</p>
<ul>
<li>The key is already in the map</li>
<li>The key has a non-<code>null</code> value associated and the function also returns a non-<code>null</code> value</li>
</ul>
<p>If the key has a non-<code>null</code> associated and the function returns <code>null</code>, the key is removed from the map. For example:</p>
<p><code class="java hljs">Map<String, String> letters = <span class="hljs-keyword">new</span> HashMap<>();<br />
letters.put(<span class="hljs-string">"a"</span>, <span class="hljs-string">"a"</span>);<br />
letters.put(<span class="hljs-string">"b"</span>, <span class="hljs-keyword">null</span>);<br />
letters.put(<span class="hljs-string">"d"</span>, <span class="hljs-string">"d"</span>);<br />
<br />
BiFunction<String, String, String> func = (k, v) -> {<br />
<span class="hljs-keyword"> if</span>(k.startsWith(<span class="hljs-string">"d"</span>)) <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;<br />
<span class="hljs-keyword"> return</span> k.toUpperCase();<br />
};<br />
<br />
<span style="color: rgb(0, 106, 0);">// Will update</span><br />
letters.computeIfPresent(<span class="hljs-string">"a"</span>, func);<br />
<span style="color: rgb(0, 106, 0);">// Won't update</span> <br />
letters.computeIfPresent(<span class="hljs-string">"b"</span>, func);<br />
<span style="color: rgb(0, 106, 0);">// Won't add</span> <br />
letters.computeIfPresent(<span class="hljs-string">"c"</span>, func); <br />
<span style="color: rgb(0, 106, 0);">// Will remove</span><br />
letters.computeIfPresent(<span class="hljs-string">"d"</span>, func); <br />
letters.forEach( (key, value) -><br />
System.out.format(<span class="hljs-string">"%s-%s;"</span>, key, value)<br />
);</code></p>
<p>In this case, the output will be:</p>
<p><code class="java hljs">a-A;b-<span class="hljs-keyword">null</span>;</code></p>
<h2>Switch statement</h2>
<p>A <code>switch</code> statement is another way to represent an <code>if-else</code> statement. This is its syntax:</p>
<p><code class="java hljs">switch(expression) {<br/>
case constant_value_1:<br/>
statements;<br/>
case constant_value_2:<br/>
statements;<br/>
default:<br/>
statements;<br/>
}</code></p>
<p>The switch expression must evaluate to one of the following types:</p>
<ul>
<li><code>byte</code>, <code>short</code>, <code>char</code>, <code>int</code></li>
<li><code>enum</code></li>
<li><code>String</code></li>
</ul>
<p>Anything else, like a <code>float</code>, will generate a compiler error.</p>
<p>Here's an example of a <code>switch</code> statement:</p>
<p><code class="java hljs">String s = <span class="hljs-string">"Jack"</span>;<br/>
<span class="hljs-keyword">switch</span>(s) {<br/>
<span class="hljs-keyword">case</span> <span class="hljs-string">"Mike"</span>:<br/>
System.out.println(<span class="hljs-string">"Good morning Mr. "</span> + s); <br/> <span class="hljs-keyword">break</span>;<br/>
<span class="hljs-keyword">case</span> <span class="hljs-string">"Laura"</span>:<br/>
System.out.println(<span class="hljs-string">"Good morning Mrs. "</span> + s); <br/> <span class="hljs-keyword">break</span>;<br/>
<span class="hljs-keyword">default</span>:<br/>
System.out.println(<span class="hljs-string">"Good morning "</span> + s); <br/>
}</code></p>
<p>There are many things to take into account with a <code>switch</code> statement.</p>
<p>First, each case needs either a constant value or a <code>final</code> variable initialized at declaration. Otherwise, a compile time error will be generated:</p>
<p><code class="java hljs"><span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> goodFinal = 2;<br/>
<span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> badFinal;<br/>
badFinal = 3;<br/>
<span class="hljs-keyword">int</span> var = 1;<br/>
<br/>
<span class="hljs-keyword">switch</span>(var) {<br/>
<span class="hljs-keyword">case</span> 1: <span class="hljs-comment">// OK</span><br/>
var *= 2; <br/> <span class="hljs-keyword">break;</span><br/>
<span class="hljs-keyword">case</span> goodFinal: <span class="hljs-comment">// OK</span><br/>
var++; <br/> <span class="hljs-keyword">break;</span><br/>
<span class="hljs-keyword">case</span> badFinal: <span class="hljs-comment">// Compiler error!</span><br/>
var--;<br/>
}</code></p>
<p>It's not valid to have more than <code>case</code> with the same value:</p>
<p><code class="java hljs"><span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> goodFinal = 2;<br/>
<span class="hljs-keyword">int</span> var = 1;<br/>
<br/>
<span class="hljs-keyword">switch</span>(var) {<br/>
<span class="hljs-keyword">case</span> 1: <span class="hljs-comment">// OK</span><br/>
var *= 2; <br/> <span class="hljs-keyword">break;</span><br/>
<span class="hljs-keyword">case</span> 2: <span class="hljs-comment">// Compiler error!</span><br/>
var++; <br/> <span class="hljs-keyword">break;</span><br/>
<span class="hljs-keyword">case</span> goodFinal: <span class="hljs-comment">// Compiler error!</span><br/>
var--;<br/>
}</code></p>
<p>Besides, <code>case</code> labels are evaluated from top to down beginning from the first <code>case</code> constant that matches the <code>switch</code> expression. This means that all the subsequent statements will be executed until the end of the <code>switch</code> statement, or a <code>break</code> is found. For example:</p>
<p><code class="java hljs"><span class="hljs-keyword">int</span> var = 1;<br/>
<br/>
<span class="hljs-keyword">switch</span>(var) {<br/>
<span class="hljs-keyword">case</span> 1:<br/>
System.out.println(<span class="hljs-string">"Shirt"</span>); <br/>
<span class="hljs-keyword">case</span> 2:<br/>
System.out.println(<span class="hljs-string">"Pants"</span>);<br/>
<span class="hljs-keyword">case</span> 3:<br/>
System.out.println(<span class="hljs-string">"Shoes"</span>);<br/>
}</code></p>
<p>Will output:</p>
<p><code class="java hljs">Shirt<br/>
Pants<br/>
Shoes
</code></p>
<p>Because of this, we can have a <span class="hljs-keyword">switch</span> statement like the following:</p>
<p><code class="java hljs"><span class="hljs-keyword">int</span> var = 1;<br/>
<br/>
<span class="hljs-keyword">switch</span>(var) {<br/>
<span class="hljs-keyword">case</span> 1:<br/>
<span class="hljs-keyword">case</span> 2:<br/>
System.out.println(<span class="hljs-string">"Pants"</span>);<br/> <span class="hljs-keyword">break;</span><br/>
<span class="hljs-keyword">case</span> 3:<br/>
System.out.println(<span class="hljs-string">"Shoes"</span>);<br/>
}</code></p>
<p>Where, if <code>var</code> is <code>1</code> or <code>2</code>, execution will <i>fall through</i>, and <code>Pants</code> will be printed.</p>
<p>Also, notice that <code>default</code> doesn't need a <code>break</code> because it's the last element of the <code>switch</code> statement. However, <code>default</code> can appear in any position, so watch for it.</p>
Finally, it may be obvious, but <code>switch</code> statement can only test for equality. This is important when using a <code>String</code> expression because we have to take into account the meaning of object equality. In other words, this won't match:
<p><code class="java hljs">String s = <span class="hljs-string">"a"</span>;<br/>
<br/>
<span class="hljs-keyword">switch</span>(s) {<br/>
<span class="hljs-keyword">case</span> <span class="hljs-string">"A"</span>: <span class="hljs-comment">// Strings are case-sensitive, so this case won't match</span><br/>
...<br/>
}</code></p>
<h2>Numeric Literals</h2>
<p>In Java, integers numbers are by default of type <code>int</code> and floating-point numbers are by default of type <code>double</code>.</p>
<p>Integer numbers are the ones with types <code>byte</code>, <code>short</code>, <code>int</code>, and <code>long</code> and generally expressed in decimal base. However, they can also be expressed as hexadecimal or binary numbers.</p>
<p>Hexadecimal numbers consist of numbers <code>0</code> through <code>9</code> and letters <code>A</code> through <code>F</code>. To specify a hexadecimal number, you add the <code>0x</code> or <code>0X</code> prefix.</p>
<p><code class="java hljs"><span class="hljs-keyword">int</span> hex = 0x2B; <span class="hljs-comment">// The number 43</span>
</code></p>
<p>Binary numbers consist of the numbers <code>0</code> and <code>1</code>. To specify a binary number, you add the <code>0b</code> or <code>0B</code> prefix.</p>
<p><code class="java hljs"><span class="hljs-keyword">int</span> bin = 0b0010110; <span class="hljs-comment">// The number 22</span>
</code></p>
<p>These numbers can be cast to <code>float</code> or <code>double</code> types, but we can't express these types as hexadecimal or binary numbers. For example:</p>
<p><code class="java hljs"><span class="hljs-keyword">float</span> goodFloat = 0xF2; <span class="hljs-comment">// OK, number 242.0</span><br/>
<span class="hljs-keyword">double</span> goodDouble = 0b11110110; ; <span class="hljs-comment">// OK, number 246.0</span><br/>
<span class="hljs-keyword">float</span> badFloat = 0xF2f; <span class="hljs-comment"> // Compilation error!</span><br/>
<span class="hljs-keyword">double</span> badDouble = 0b11110110d; ; <span class="hljs-comment">// Compilation error!</span>
</code></p>
<p>From Java 7, you can place any number of underscores between the digits of numbers to separate groups of digits and improve readability.</p>
<p>You cannot put underscores in the following places:</p>
<ul>
<li>At the beginning or end of a number</li>
<li>Adjacent to a decimal point in a floating point literal</li>
<li>Before an F or L suffix</li>
<li>In positions where a string of digits is expected</li>
</ul>
<p>Here are some examples:</p>
<p><code class="java hljs"><span class="hljs-keyword">int</span> i = 34_765; <span class="hljs-comment">// OK</span><br/>
<span class="hljs-keyword">float</span> f = 43.987_876; <span class="hljs-comment">// OK</span><br/>
<span class="hljs-keyword">short</span> s = 0b0000____0101; <span class="hljs-comment">// OK</span><br/>
<span class="hljs-keyword">int</span> i2 = 100_; <span class="hljs-comment">// Compilation error!</span><br/>
<span class="hljs-keyword">int</span> i3 = 0_x1D; <span class="hljs-comment">// Compilation error!</span><br/>
<span class="hljs-keyword">int</span> i4 = 0x_1D; <span class="hljs-comment">// Compilation error!</span><br/>
<span class="hljs-keyword">float</span> f2 = 8945.40_f; <span class="hljs-comment">// Compilation error!</span><br/>
<span class="hljs-keyword">float</span> f3 = 8945_.40f; <span class="hljs-comment">// Compilation error!</span><br/>
</code></p>
<h2>java.util.concurrent.locks</h2>
<h4>Soon...</h4>
<h2>Formatting dates, numbers, and currency values</h2>
<h4>Soon...</h4>
<h2>DirectoryStream and FileVisitor interfaces</h2>
<h4>Soon...</h4>
<h2>PathMatcher</h2>
<h4>Soon...</h4>
<h2>WatchService </h2>
<h4>Soon...</h4>
<div class="answers">
<a href="ch31a.html" target="_blank">Open answers page</a>
</div>
<div class="book-info"></div>
<div class="linkbox">
<div class="previous">
<a href="ch30.html">30. Localization</a>
</div>
<div class="next">
</div>
<div style="clear:both;"></div>
</div>
</div>
</div>
</body>
</html>