-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathch10.html
1021 lines (673 loc) · 69.2 KB
/
ch10.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!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">Chapter TEN</i><br />
Java Built-In Lambda Interfaces</h1>
<p><br /></p>
<h3 style="text-align: center;"><i>Exam Objectives</i></h3>
<p style="text-align: center;"><i>Use the built-in interfaces included in the java.util.function package such as Predicate, Consumer, Function, and Supplier.<br />
Develop code that uses primitive versions of functional interfaces.<br />
Develop code that uses binary versions of functional interfaces.<br />
Develop code that uses the UnaryOperator interface.</i></p>
</div>
</div>
</div>
<div class="container">
<div class="column">
<h2>Why built-in interfaces?</h2>
<p>A lambda expression must correspond to one functional interface.</p>
<p>We can use any interface as a lambda expression as long as the interface only contains one abstract method.</p>
<p>We also saw that the Java 8 API has many functional interfaces that we can use to construct lambda expressions, like <code>java.lang.Runnable</code> or <code>java.lang.Comparable</code>.</p>
<p>However, Java 8 contains new functional interfaces to work specifically with lambda expressions, covering the most common scenarios usages.</p>
<p>For example, two common scenarios are to filter things based on a particular condition and test for some condition on the properties of an object.</p>
<p>In the previous chapters, we used:</p>
<p><code class="java hljs"><span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Searchable</span></span> {<br />
<span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">test</span><span class="hljs-params">(Car c)</span></span>;<br />
}</code></p>
<p>But the problem is that we have to write an interface like that in each program that uses it (or link a library that contains it).</p>
<p>Luckily, an interface that does the same but accepts any object type already exists in the language.</p>
<p>The new functional interfaces are located inside the <code>java.util.function</code> package.</p>
<p>There are five of them:</p>
<ul>
<li><code><b>Predicate<T></b></code></li>
<li><code><b>Consumer<T></b></code></li>
<li><code><b>Function<T, R></b></code></li>
<li><code><b>Supplier<T></b></code></li>
<li><code><b>UnaryOperator<T></b></code></li>
</ul>
<p>Where <code>T</code> and <code>R</code> represent generic types (<code>T</code> represents a parameter type and <code>R</code> the return type).</p>
<p>Also, they also have specializations for the cases where the input parameter is a primitive type (actually just for <code>int</code>, <code>long</code>, <code>double</code>, and <code>boolean</code> just in the case of <code>Supplier</code>), for example:</p>
<ul>
<li><code><b>IntPredicate</b></code></li>
<li><code><b>LongConsumer</b></code></li>
<li><code><b>BooleanSupplier</b></code></li>
</ul>
<p>Where the name is preceded by the appropriate primitive type.</p>
<p>Plus, four of them have binary versions, which means they take two parameters instead of one:</p>
<ul>
<li><code><b>BiPredicate<L, R></b></code></li>
<li><code><b>BiConsumer<T, U></b></code></li>
<li><code><b>BiFunction<T, U, R></b></code></li>
<li><code><b>BinaryOperator<T></b></code></li>
</ul>
<p>Where <code>T</code>, <code>U</code>, and <code>R</code> represent generic types (<code>T</code> and <code>U</code> represent parameter types and <code>R</code> the return type).</p>
<p>The following tables show the complete list of interfaces.</p>
<p>You don't have to memorize them, just try to understand them.</p>
<p>In the following pages, each interface will be explained.</p>
<table border="1" cellpadding="3" width="100%">
<tr>
<th>Functional Interface</th>
<th>Primitive Versions</th>
</tr>
<tr>
<td><code>Predicate<T></code></td>
<td><code>IntPredicate</code><br />
<code>LongPredicate</code><br />
<code>DoublePredicate</code></td>
</tr>
<tr>
<td><code>Consumer<T></code></td>
<td><code>IntConsumer</code><br />
<code>LongConsumer</code><br />
<code>DoubleConsumer</code></td>
</tr>
<tr>
<td><code>Function<T, R></code></td>
<td><code>IntFunction<R></code><br />
<code>IntToDoubleFunction</code><br />
<code>IntToLongFunction</code><br />
<code>LongFunction<R></code><br />
<code>LongToDoubleFunction</code><br />
<code>LongToIntFunction</code><br />
<code>DoubleFunction<R></code><br />
<code>DoubleToIntFunction</code><br />
<code>DoubleToLongFunction</code><br />
<code>ToIntFunction<T></code><br />
<code>ToDoubleFunction<T></code><br />
<code>ToLongFunction<T></code></td>
</tr>
<tr>
<td><code>Supplier<T></code></td>
<td><code>BooleanSupplier</code><br />
<code>IntSupplier</code><br />
<code>LongSupplier</code><br />
<code>DoubleSupplier</code></td>
</tr>
<tr>
<td><code>UnaryOperator<T></code></td>
<td><code>IntUnaryOperator</code><br />
<code>LongUnaryOperator</code><br />
<code>DoubleUnaryOperator</code></td>
</tr>
</table>
<p><br /></p>
<table border="1" cellpadding="3" width="100%">
<tr>
<th>Functional Interface</th>
<th>Primitive Versions</th>
</tr>
<tr>
<td><code>BiPredicate<L, R></code></td>
<td></td>
</tr>
<tr>
<td><code>BiConsumer<T, U></code></td>
<td><code>ObjIntConsumer<T></code><br />
<code>ObjLongConsumer<T></code><br />
<code>ObjDoubleConsumer<T></code></td>
</tr>
<tr>
<td><code>BiFunction<T, U, R></code></td>
<td><code>ToIntBiFunction<T, U></code><br />
<code>ToLongBiFunction<T, U></code><br />
<code>ToDoubleBiFunction<T, U></code></td>
</tr>
<tr>
<td><code>BinaryOperator<T></code></td>
<td><code>IntBinaryOperator</code><br />
<code>LongBinaryOperator</code><br />
<code>DoubleBinaryOperator</code></td>
</tr>
</table>
<h2>Predicate</h2>
<p>A predicate is a statement that may be true or false depending on the values of its variables.</p>
<p>This functional interface can be used anywhere you need to evaluate a <code>boolean</code> condition.</p>
<p>This how the interface is defined:</p>
<p><code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Predicate</span><<span class="hljs-title">T</span>></span> {<br />
<span class="hljs-function"><span class="hljs-keyword"> boolean</span> <span class="hljs-title">test</span><span class="hljs-params">(T t)</span></span>;<br />
<span class="hljs-comment">// Other default and static methods<br /></span><span class="hljs-comment"> // ...</span><br />
}</code></p>
<p>So the functional descriptor (method signature) is:</p>
<p><code class="java hljs">T <span class="hljs-subst">-></span> <span class="hljs-built_in">boolean</span></code></p>
<p>Here's an example using an anonymous class:</p>
<p><code class="java hljs">Predicate<String> startsWithA = <span class="hljs-keyword">new</span> Predicate<String>() {<br />
<span class="hljs-annotation"> @Override<font color="#000000"><br /></font></span><span class="hljs-function"><span class="hljs-keyword"><font color="#000000"> </font>public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">test</span><span class="hljs-params">(String t)</span></span> {<br />
<span class="hljs-keyword"> return</span> t.startsWith(<span class="hljs-string">"A"</span>);<br />
}<br />
};<br />
<span class="hljs-keyword">boolean</span> result = startsWithA.test(<span class="hljs-string">"Arthur"</span>);</code></p>
<p>And with a lambda expression:</p>
<p><code class="java hljs">Predicate<String> startsWithA = t -> t.startsWith(<span class="hljs-string">"A"</span>);<br />
<span class="hljs-keyword">boolean</span> result = startsWithA.test(<span class="hljs-string">"Arthur"</span>);</code></p>
<p>This interface also has the following default methods:</p>
<p><code class="java hljs"><span class="hljs-function"><span class="hljs-keyword">default</span> Predicate<T> <span class="hljs-title">and</span><span class="hljs-params">(Predicate<? <span class="hljs-keyword">super</span> T> other)</span><br />
<span class="hljs-keyword">default</span> Predicate<T> <span class="hljs-title">or</span><span class="hljs-params">(Predicate<? <span class="hljs-keyword">super</span> T> other)</span><br />
<span class="hljs-keyword">default</span> Predicate<T> <span class="hljs-title">negate</span><span class="hljs-params">()</span></span></code></p>
<p>These methods return a composed <code>Predicate</code> that represents a short-circuiting logical <b>AND</b> and <b>OR</b> of this predicate and another and its logical negation.</p>
<p>Short-circuiting means that the other predicate won't be evaluated if the value of the first predicate can predict the result of the operation (if the first predicate return false in the case of <b>AND</b> or if it returns true in the case of <b>OR</b>).</p>
<p>These methods are useful to combine predicates and make the code more readable, for example:</p>
<p><code class="java hljs">Predicate<String> startsWithA = t -> t.startsWith(<span class="hljs-string">"A"</span>);<br />
Predicate<String> endsWithA = t -> t.endsWith(<span class="hljs-string">"A"</span>);<br />
<span class="hljs-keyword">boolean</span> result = startsWithA.and(endsWithA).test(<span class="hljs-string">"Hi"</span>);</code></p>
<p>Also there's a <code>static</code> method:</p>
<p><code class="java hljs"><span class="hljs-keyword">static</span> <T> <span class="hljs-function">Predicate<T> <span class="hljs-title">isEqual</span><span class="hljs-params">(Object targetRef)</span></span></code></p>
<p>That returns a <code>Predicate</code> that tests if two arguments are equal according to <code>Objects.equals(Object, Object)</code>.</p>
<p>There are also primitive versions for <code>int</code>, <code>long</code> and <code>double</code>. They don't extend from <code>Predicate</code>.</p>
<p>For example, here's the definition of <code>IntPredicate</code>:</p>
<p><code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">IntPredicate</span></span> {<br />
<span class="hljs-function"><span class="hljs-keyword"> boolean</span> <span class="hljs-title">test</span><span class="hljs-params">(<span class="hljs-keyword">int</span> value)</span></span>;<br />
<span class="hljs-comment">// And the default methods: and, or, negate</span><br />
}</code></p>
<p>So instead of using:</p>
<p><code class="java hljs">Predicate<Integer> even = t -> t % <span class="hljs-number">2</span> == <span class="hljs-number">1</span>;<br />
<span class="hljs-keyword">boolean</span> result = even.test(<span class="hljs-number">5</span>);</code></p>
<p>You can use:</p>
<p><code class="java hljs">IntPredicate even = t -> t % <span class="hljs-number">2</span> == <span class="hljs-number">1</span>;<br />
<span class="hljs-keyword">boolean</span> result = even.test(<span class="hljs-number">5</span>);</code></p>
<p>Why?</p>
<p>Just to avoid the conversion from <code>Integer</code> to <code>int</code> and work directly with primitive types.</p>
<p>Notice that these primitive versions don't have a generic type. Due to the way generics are implemented, parameters of the functional interfaces can be bound only to object types.</p>
<p>Since the conversion from the wrapper type (<code>Integer</code>) to the primitive type (<code>int</code>) uses more memory and comes with a performance cost, Java provides these versions to avoid autoboxing operations when inputs or outputs are primitives.</p>
<h2>Consumer</h2>
<p>A consumer is an operation that accepts a single input argument and returns no result; it just execute some operations on the argument.</p>
<p>This how the interface is defined:</p>
<p><code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Consumer</span><<span class="hljs-title">T</span>></span> {<br />
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">accept</span><span class="hljs-params">(T t)</span></span>;<br />
<span class="hljs-comment">// And a default method<br /></span> <span class="hljs-comment">// ...</span><br />
}</code></p>
<p>So the functional descriptor (method signature) is:</p>
<p><code class="java hljs">T -> <span class="hljs-keyword">void</span></code></p>
<p>Here's an example using an anonymous class:</p>
<p><code class="java hljs">Consumer<String> consumeStr = <span class="hljs-keyword">new</span> Consumer<String>() {<br />
<span class="hljs-annotation">@Override<br /></span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">accept</span><span class="hljs-params">(String t)</span></span> {<br />
System.out.println(t);<br />
}<br />
};<br />
consumeStr.accept(<span class="hljs-string">"Hi"</span>);</code></p>
<p>And with a lambda expression:</p>
<p><code class="java hljs">Consumer<String> consumeStr = t -> System.out.println(t);<br />
consumeStr.accept(<span class="hljs-string">"Hi"</span>);</code></p>
<p>This interface also has the following default method:</p>
<p><code class="java hljs"><span class="hljs-function"><span class="hljs-keyword">default</span> Consumer<T> <span class="hljs-title">andThen</span><span class="hljs-params">(Consumer<? <span class="hljs-keyword">super</span> T> after)</span></span></code></p>
<p>This method returns a composed <code>Consumer</code> that performs, in sequence, the operation of the consumer followed by the operation of the parameter.</p>
<p>These methods are useful to combine <code>Consumer</code>s and make the code more readable, for example:</p>
<p><code class="java hljs">Consumer<String> first = t -><br />
System.out.println(<span class="hljs-string">"First:"</span> + t);<br />
Consumer<String> second = t -><br />
System.out.println(<span class="hljs-string">"Second:"</span> + t);<br />
first.andThen(second).accept(<span class="hljs-string">"Hi"</span>);</code></p>
<p>The output is:</p>
<p><code class="java hljs">First: Hi<br />
Second: Hi</code></p>
<p>Look how both <code>Consumer</code>s take the same argument and the order of execution.</p>
<p>There are also primitive versions for <code>int</code>, <code>long</code> and <code>double</code>. They don't extend from <code>Consumer</code>.</p>
<p>For example, here's the definition of <code>IntConsumer</code>:</p>
<p><code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">IntConsumer</span></span> {<br />
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">accept</span><span class="hljs-params">(<span class="hljs-keyword">int</span> value)</span></span>;<br />
<span class="hljs-function"><span class="hljs-keyword">default</span> IntConsumer <span class="hljs-title">andThen</span><span class="hljs-params">(IntConsumer after)</span></span> {<br />
<span class="hljs-comment">// ...<br /></span> }<br />
}</code></p>
<p>So instead of using:</p>
<p><code class="java hljs"><span class="hljs-keyword">int</span>[] a = {<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>,<span class="hljs-number">8</span>};<br />
printList(a, t -> System.out.println(t));<br />
<span class="hljs-comment">//...</span><br />
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printList</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] a, Consumer<Integer> c)</span></span> {<br />
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i : a) {<br />
c.accept(i);<br />
}<br />
}</code></p>
<p>You can use:</p>
<p><code class="java hljs"><span class="hljs-keyword">int</span>[] a = {<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>,<span class="hljs-number">8</span>};<br />
printList(a, t -> System.out.println(t));<br />
<span class="hljs-comment">//...</span><br />
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printList</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] a, IntConsumer c)</span></span> {<br />
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i : a) {<br />
c.accept(i);<br />
}<br />
}</code></p>
<h2>Function</h2>
<p>A function represents an operation that takes an input argument of a certain type and produces a result of another type.</p>
<p>A common use is to convert or transform from one object to another.</p>
<p>This how the interface is defined:</p>
<p><code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Function</span><<span class="hljs-title">T</span>, <span class="hljs-title">R</span>></span> {<br />
<span class="hljs-function">R <span class="hljs-title">apply</span><span class="hljs-params">(T t)</span></span>;<br />
<span class="hljs-comment">// Other default and static methods<br /></span> <span class="hljs-comment">// ...</span><br />
}</code></p>
<p>So the functional descriptor (method signature) is:</p>
<p><code class="java hljs">T -> R</code></p>
<p>Assuming a method:</p>
<p><code class="java hljs"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">round</span><span class="hljs-params">(<span class="hljs-keyword">double</span> d, Function<Double, Long> f)</span></span> {<br />
<span class="hljs-keyword">long</span> result = f.apply(d);<br />
System.out.println(result);<br />
}</code></p>
<p>Here's an example using an anonymous class:</p>
<p><code class="java hljs">round(<span class="hljs-number">5.4</span>, <span class="hljs-keyword">new</span> Function<Double, Long>() {<br />
<span class="hljs-function">Long <span class="hljs-title">apply</span><span class="hljs-params">(Double d)</span></span> {<br />
<span class="hljs-keyword">return</span> Math.round(d);<br />
}<br />
});</code></p>
<p>And with a lambda expression:</p>
<p><code class="java hljs">round(<span class="hljs-number">5.4</span>, d -> Math.round(d));</code></p>
<p>This interface also has the following default methods:</p>
<p><code class="java hljs"><span class="hljs-keyword">default</span> <V> Function<V,R> compose(<br />
Function<? <span class="hljs-keyword">super</span> V,? extends T> before)<br />
<span class="hljs-keyword">default</span> <V> Function<T,V> andThen(<br />
Function<? <span class="hljs-keyword">super</span> R,? extends V> after)</code></p>
<p>The difference between these methods is that <code>compose</code> applies the function represented by the parameter first, and its result serves as the input to the other function. <code>andThen</code> first applies the function that calls the method, and its result acts as the input of the function represented by the parameter.</p>
<p>For example:</p>
<p><code class="java hljs">Function<String, String> f1 = s -> {<br />
<span class="hljs-keyword">return</span> s.toUpperCase();<br />
};<br />
Function<String, String> f2 = s -> {<br />
<span class="hljs-keyword">return</span> s.toLowerCase();<br />
};<br />
System.out.println(f1.compose(f2).apply(<span class="hljs-string">"Compose"</span>));<br />
System.out.println(f1.andThen(f2).apply(<span class="hljs-string">"AndThen"</span>));</code></p>
<p>The output is:</p>
<p><code class="java hljs">COMPOSE<br />
andthen</code></p>
<p>In the first case, <code>f1</code> is the last function to be applied.<br /> In the second case, <code>f2</code> is the last function to be applied.</p>
<p>Also there's a <code>static</code> method:</p>
<p><code class="java hljs"><span class="hljs-keyword">static</span> <T> Function<T, T> identity()</code></p>
<p>That returns a function that always returns its input argument.</p>
<p>In the case of primitive versions, they also apply to <code>int</code>, <code>long</code> and <code>double</code>, but there are more combinations than the previous interfaces:</p>
<ul>
<li>To indicate that the function returns a generic type and takes a primitive argument, the interface is named <b>XXXFunction</b>, for example, <code>IntFunction</code>: <code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">IntFunction</span><<span class="hljs-title">R</span>></span> {<br />
<span class="hljs-function">R <span class="hljs-title">apply</span><span class="hljs-params">(<span class="hljs-keyword">int</span> value)</span></span>;<br />
}</code></li>
<li>To indicate that the function returns a primitive type and takes a generic argument, the interface is named <b>ToXXXFunction</b>, for example, <code>ToIntFunction</code>: <code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">ToIntFunction</span><<span class="hljs-title">T</span>></span> {<br />
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">applyAsInt</span><span class="hljs-params">(T value)</span></span>;<br />
}</code></li>
<li>To indicate that the function takes a primitive argument and returns another primitive type, the interface is named <b>XXXToYYYFunction</b>, where <b>XXX</b> is the argument type and <b>YYY</b> is the return type, for example, <code>IntToDoubleFunction</code>: <code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">IntToDoubleFunction</span></span> {<br />
<span class="hljs-function"><span class="hljs-keyword">double</span> <span class="hljs-title">applyAsDouble</span><span class="hljs-params">(<span class="hljs-keyword">int</span> value)</span></span>;<br />
}</code></li>
</ul>
<p>Remember that these interfaces are for convenience, to work directly with primitives, for example:</p>
<p><code>DoubleFunction<R></code> instead of <code>Function<Double, R></code><br />
<code>ToLongFunction<T></code> instead of <code>Function<T, Long></code><br />
<code>IntToLongFunction</code> instead of <code>Function<Integer, Long></code></p>
<h2>Supplier</h2>
<p>A supplier does the opposite of a consumer, it takes no arguments and only returns some value.</p>
<p>This how the interface is defined:</p>
<p><code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Supplier</span><<span class="hljs-title">T</span>></span> {<br />
<span class="hljs-function">T <span class="hljs-title">get</span><span class="hljs-params">()</span></span>;<br />
}</code></p>
<p>So the functional descriptor (method signature) is:</p>
<p><code class="java hljs">() -> T</code></p>
<p>Here's an example using an anonymous class:</p>
<p><code class="java hljs">String t = <span class="hljs-string">"One"</span>;<br />
Supplier<String> supplierStr = <span class="hljs-keyword">new</span> Supplier<String>() {<br />
<span class="hljs-annotation">@Override<br /></span> <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">get</span><span class="hljs-params">()</span></span> {<br />
<span class="hljs-keyword">return</span> t.toUpperCase();<br />
}<br />
};<br />
System.out.println(supplierStr.get());</code></p>
<p>And with a lambda expression:</p>
<p><code class="java hljs">String t = <span class="hljs-string">"One"</span>;<br />
Supplier<String> supplierStr = () -> t.toUpperCase();<br />
System.out.println(supplierStr.get());</code></p>
<p>This interface doesn't define default methods.</p>
<p>There are also primitive versions for <code>int</code>, <code>long</code> and <code>double</code> and <code>boolean</code>. They don't extend from <code>Supplier</code>.</p>
<p>For example, here's the definition of <code>BooleanSupplier</code>:</p>
<p><code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">BooleanSupplier</span></span> {<br />
<span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">getAsBoolean</span><span class="hljs-params">()</span></span>;<br />
}</code></p>
<p>That can be used instead of <code>Supplier</code>.</p>
<h2>UnaryOperator</h2>
<p><code>UnaryOperator</code> is just a specialization of the <code>Function</code> interface (in fact, this interface extends from it) for when the argument and the result are of the same type.</p>
<p>This how the interface is defined:</p>
<p><code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">UnaryOperator</span><<span class="hljs-title">T</span>><br />
<span class="hljs-keyword">extends</span> <span class="hljs-title">Function</span><<span class="hljs-title">T</span>, <span class="hljs-title">T</span>></span> {<br />
<span class="hljs-comment">// Just the identity<br />
</span> <span class="hljs-comment">// method is defined</span><br />
}</code></p>
<p>So the functional descriptor (method signature) is:</p>
<p><code class="java hljs">T -> T</code></p>
<p>Here's an example using an anonymous class:</p>
<p><code class="java hljs">UnaryOperator<String> uOp = <span class="hljs-keyword">new</span> UnaryOperator<String>() {<br />
<span class="hljs-annotation">@Override</span><br /><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">apply</span><span class="hljs-params">(String t)</span></span> {<br />
<span class="hljs-keyword">return</span> t.substring(<span class="hljs-number">0</span>,<span class="hljs-number">2</span>);<br />
}<br />
};<br />
System.out.println(uOp.apply(<span class="hljs-string">"Hello"</span>));</code></p>
<p>And with a lambda expression:</p>
<p><code class="java hljs">UnaryOperator<String> uOp = t -> t.substring(<span class="hljs-number">0</span>,<span class="hljs-number">2</span>);<br />
System.out.println(uOp.apply(<span class="hljs-string">"Hello"</span>));</code></p>
<p>This interface inherits the default methods of the <code>Function</code> interface:</p>
<p><code class="java hljs"><span class="hljs-keyword">default</span> <V> Function<V,R> compose(<br />
Function<? <span class="hljs-keyword">super</span> V,? extends T> before)<br />
<span class="hljs-keyword">default</span> <V> Function<T,V> andThen(<br />
Function<? <span class="hljs-keyword">super</span> R,? extends V> after)</code></p>
<p>And just defines the <code>static</code> method <code>identity()</code> for this interface (since <code>static</code> methods are not inherited):</p>
<p><code class="java hljs"><span class="hljs-keyword">static</span> <T> <span class="hljs-function">UnaryOperator<T> <span class="hljs-title">identity</span><span class="hljs-params">()</span></span></code></p>
<p>That returns an <code>UnaryOperator</code> that always returns its input argument.</p>
<p>There are also primitive versions for <code>int</code>, <code>long</code> and <code>double</code>. They don't extend from <code>UnaryOperator</code>.</p>
<p>For example, here's the definition of <code>IntUnaryOperator</code>:</p>
<p><code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">IntUnaryOperator</span></span> {<br />
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">applyAsInt</span><span class="hljs-params">(<span class="hljs-keyword">int</span> value)</span></span>;<br />
<span class="hljs-comment">// Definitions for compose, andThen, and identity<br /></span>}</code></p>
<p>So instead of using:</p>
<p><code class="java hljs"><span class="hljs-keyword">int</span>[] a = {<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>,<span class="hljs-number">8</span>};<br />
<span class="hljs-keyword">int</span> sum = sumNumbers(a, t -> t * <span class="hljs-number">2</span>);<br />
<span class="hljs-comment">//...</span><br />
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">sumNumbers</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] a, UnaryOperator<Integer> unary)</span></span> {<br />
<span class="hljs-keyword">int</span> sum = <span class="hljs-number">0</span>;<br />
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i : a) {<br />
sum += unary.apply(i);<br />
}<br />
<span class="hljs-keyword">return</span> sum;<br />
}</code></p>
<p>You can use:</p>
<p><code class="java hljs"><span class="hljs-keyword">int</span>[] a = {<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>,<span class="hljs-number">8</span>};<br />
<span class="hljs-keyword">int</span> sum = sumNumbers(a, t -> t * <span class="hljs-number">2</span>);<br />
<span class="hljs-comment">//...</span><br />
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">sumNumbers</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] a, IntUnaryOperator unary)</span></span> {<br />
<span class="hljs-keyword">int</span> sum = <span class="hljs-number">0</span>;<br />
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i : a) {<br />
sum += unary.applyAsInt(i);<br />
}<br />
<span class="hljs-keyword">return</span> sum;<br />
}</code></p>
<h2>BiPredicate</h2>
<p>This interface represents a predicate that takes two arguments.</p>
<p>This how the interface is defined:</p>
<p><code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span> <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">BiPredicate</span><<span class="hljs-title">T</span>, <span class="hljs-title">U</span>></span> {<br />
<span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">test</span><span class="hljs-params">(T t, U u)</span></span>;<br />
<span class="hljs-comment">// Default methods are defined also</span><br />
}</code></p>
<p>So the functional descriptor (method signature) is:</p>
<p><code class="java hljs">(T, U) -> <span class="hljs-keyword">boolean</span></code></p>
<p>Here's an example using an anonymous class:</p>
<p><code class="java hljs">BiPredicate<Integer, Integer> divisible =<br />
<span class="hljs-keyword">new</span> BiPredicate<Integer, Integer>() {<br />
<span class="hljs-annotation">@Override<br /></span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">test</span><span class="hljs-params">(Integer t, Integer u)</span></span> {<br />
<span class="hljs-keyword">return</span> t % u == <span class="hljs-number">0</span>;<br />
}<br />
};<br />
<span class="hljs-keyword">boolean</span> result = divisible.test(<span class="hljs-number">10</span>, <span class="hljs-number">5</span>);</code></p>
<p>And with a lambda expression:</p>
<p><code class="java hljs">BiPredicate<Integer, Integer> divisible =<br />
(t, u) -> t % u == <span class="hljs-number">0</span>;<br />
<span class="hljs-keyword">boolean</span> result = divisible.test(<span class="hljs-number">10</span>, <span class="hljs-number">5</span>);</code></p>
<p>This interface defines the same default methods of the Predicate interface, but with two arguments:</p>
<p><code class="java hljs"><span class="hljs-keyword">default</span> BiPredicate<T, U> and(<br />
BiPredicate<? <span class="hljs-keyword">super</span> T, ? <span class="hljs-keyword">super</span> U> other)<br />
<span class="hljs-keyword">default</span> BiPredicate<T, U> or(<br />
BiPredicate<? <span class="hljs-keyword">super</span> T, ? <span class="hljs-keyword">super</span> U> other)<br />
<span class="hljs-keyword">default</span> BiPredicate<T, U> negate()</code></p>
<p>This interface doesn't have primitive versions.</p>
<h2>BiConsumer</h2>
<p>This interface represents a consumer that takes two arguments (and don't return a result).</p>
<p>This how the interface is defined:</p>
<p><code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">BiConsumer</span><<span class="hljs-title">T</span>, <span class="hljs-title">U</span>></span> {<br />
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">accept</span><span class="hljs-params">(T t, U u)</span></span>;<br />
<span class="hljs-comment">// andThen default method is defined</span><br />
}</code></p>
<p>So the functional descriptor (method signature) is:</p>
<p><code class="java hljs">(T, U) -> <span class="hljs-keyword">void</span></code></p>
<p>Here's an example using an anonymous class:</p>
<p><code class="java hljs">BiConsumer<String, String> consumeStr =<br />
<span class="hljs-keyword">new</span> BiConsumer<String, String>() {<br />
<span class="hljs-annotation">@Override<br /></span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">accept</span><span class="hljs-params">(String t, String u)</span></span> {<br />
System.out.println(t + <span class="hljs-string">" "</span> + u);<br />
}<br />
};<br />
consumeStr.accept(<span class="hljs-string">"Hi"</span>, <span class="hljs-string">"there"</span>);</code></p>
<p>And with a lambda expression:</p>
<p><code class="java hljs">BiConsumer<String> consumeStr =<br />
(t, u) -> System.out.println(t + <span class="hljs-string">" "</span> + u);<br />
consumeStr.accept(<span class="hljs-string">"Hi"</span>, <span class="hljs-string">"there"</span>);</code></p>
<p>This interface also has the following default method:</p>
<p><code class="java hljs"><span class="hljs-keyword">default</span> BiConsumer<T, U> andThen(<br />
BiConsumer<? <span class="hljs-keyword">super</span> T, ? <span class="hljs-keyword">super</span> U> after)</code></p>
<p>This method returns a composed <code>BiConsumer</code> that performs, in sequence, the operation of the consumer followed by the operation of the parameter.</p>
<p>As in the case of a <code>Consumer</code>, these methods are useful to combine <code>BiConsumer</code>s and make the code more readable, for example:</p>
<p><code class="java hljs">BiConsumer<String, String> first = (t, u) -> System.out.println(t.toUpperCase() + u.toUpperCase());<br />
BiConsumer<String, String> second = (t, u) -> System.out.println(t.toLowerCase() + u.toLowerCase());<br />
first.andThen(second).accept(<span class="hljs-string">"Again"</span>, <span class="hljs-string">" and again"</span>);</code></p>
<p>The output is:</p>
<p><code class="java hljs">AGAIN AND AGAIN again and again</code></p>
<p>There are also primitive versions for <code>int</code>, <code>long</code> and <code>double</code>. They don't extend from <code>BiConsumer</code>, and instead of taking two <code>int</code>s, for example, they take one object and a primitive value as a second argument. So the naming convention changes to <b>ObjXXXConsumer</b>, where <b>XXX</b> is the primitive type. For example, here's the definition of <code>ObjIntConsumer</code>:</p>
<p><code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">ObjIntConsumer</span><<span class="hljs-title">T</span>></span> {<br />
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">accept</span><span class="hljs-params">(T t, <span class="hljs-keyword">int</span> value)</span></span>;<br />
}</code></p>
<p>So instead of using:</p>
<p><code class="java hljs"><span class="hljs-keyword">int</span>[] a = {<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>,<span class="hljs-number">8</span>};<br />
printList(a, (t, i) -> System.out.println(t + i));<br />
<span class="hljs-comment">//...</span><br />
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printList</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] a, BiConsumer<String, Integer> c)</span></span> {<br />
<span class="hljs-keyword"> for</span>(<span class="hljs-keyword">int</span> i : a) {<br />
c.accept(<span class="hljs-string">"Number:"</span>, i);<br />
}<br />
}</code></p>
<p>You can use:</p>
<p><code class="java hljs"><span class="hljs-keyword">int</span>[] a = {<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>,<span class="hljs-number">8</span>};<br />
printList(a, (t, i) -> System.out.println(t + i));<br />
<span class="hljs-comment">//...</span><br />
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printList</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] a, ObjIntConsumer<String> c)</span></span> {<br />
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i : a) {<br />
c.accept(<span class="hljs-string">"Number:"</span>, i);<br />
}<br />
}</code></p>
<h2>BiFunction</h2>
<p>This interface represents a function that takes two arguments of different type and produces a result of another type.</p>
<p>This how the interface is defined:</p>
<p><code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">BiFunction</span><<span class="hljs-title">T</span>, <span class="hljs-title">U</span>, <span class="hljs-title">R</span>></span> {<br />
<span class="hljs-function">R <span class="hljs-title">apply</span><span class="hljs-params">(T t, U u)</span></span>; <span class="hljs-comment">// Other default and static methods<br /></span> <span class="hljs-comment">// ...</span><br />
}</code></p>
<p>So the functional descriptor (method signature) is:</p>
<p><code class="java hljs">(T, U) -> R</code></p>
<p>Assuming a method:</p>
<p><code class="java hljs"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">round</span><span class="hljs-params">(<span class="hljs-keyword">double</span> d1, <span class="hljs-keyword">double</span> d2, BiFunction<Double, Double, Long> f)</span></span> {<br />
<span class="hljs-keyword">long</span> result = f.apply(d1, d2);<br />
System.out.println(result);<br />
}</code></p>
<p>Here's an example using an anonymous class:</p>
<p><code class="java hljs">round(<span class="hljs-number">5.4</span>, <span class="hljs-number">3.8</span>, <span class="hljs-keyword">new</span> BiFunction<Double, Double, Long>() {<br />
<span class="hljs-function">Long <span class="hljs-title">apply</span><span class="hljs-params">(Double d1, Double d2)</span></span> {<br />
<span class="hljs-keyword">return</span> Math.round(d1 + d2);<br />
}<br />
});</code></p>
<p>And with a lambda expression:</p>
<p><code class="java hljs">round(<span class="hljs-number">5.4</span>, <span class="hljs-number">3.8</span>, (d1, d2) -> Math.round(d1, d2));</code></p>
<p>This interface, unlike <code>Function</code>, has only one default method:</p>
<p><code class="java hljs"><span class="hljs-keyword">default</span> <V> BiFunction<T, U, V> andThen(Function<? <span class="hljs-keyword">super</span> R, ? extends V> after)</code></p>
<p>That returns a composed function that first applies the function that calls <code>andThen</code> to its input, to finally apply the function represented by the argument to the result.</p>
<p>This interface also has less primitive versions than <code>Function</code>. It only has the versions that take generic types as arguments and return <code>int</code>, <code>long</code> and <code>double</code> primitive types, with the naming convention <b>ToXXXBiFunction</b>, where XXX is the primitive type.</p>
<p>For example, here's the definition of <code>ToIntBiFunction</code>:</p>
<p><code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">ToIntBiFunction</span><<span class="hljs-title">T</span>, <span class="hljs-title">U</span>></span> {<br />
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">applyAsInt</span><span class="hljs-params">(T t, U u)</span></span>;<br />
}</code></p>
<p>That replaces <code>BiFunction</code>.</p>
<h2>BinaryOperator</h2>
<p>This interface is a specialization of the <code>BiFunction</code> interface (in fact, this interface extends from it) for when the arguments and the result are of the same type.</p>
<p>This how the interface is defined:</p>
<p><code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">BinaryOperator</span><<span class="hljs-title">T</span>><br />
<span class="hljs-keyword">extends</span> <span class="hljs-title">BiFunction</span><<span class="hljs-title">T</span>,<span class="hljs-title">T</span>,<span class="hljs-title">T</span>></span> {<br />
<span class="hljs-comment">// Two static method are defined</span><br />
}</code></p>
<p>So the functional descriptor (method signature) is:</p>
<p><code class="java hljs">(T, T) -> T</code></p>
<p>Here's an example using an anonymous class:</p>
<p><code class="java hljs">BinaryOperator<String> binOp = <span class="hljs-keyword">new</span> BinaryOperator<String>() {<br />
<span class="hljs-annotation">@Override<br /></span> <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">apply</span><span class="hljs-params">(String t, String u)</span></span> {<br />
<span class="hljs-keyword">return</span> t.concat(u);<br />
}<br />
};<br />
System.out.println(binOp.apply(<span class="hljs-string">"Hello"</span>, <span class="hljs-string">" there"</span>));</code></p>
<p>And with a lambda expression:</p>
<p><code class="java hljs">BinaryOperator<String> binOp = (t, u) -> t.concat(u);<br />
System.out.println(binOp.apply(<span class="hljs-string">"Hello"</span>, <span class="hljs-string">" there"</span>));</code></p>
<p>This interface inherits the default method of the <code>BiFunction</code> interface:</p>
<p><code class="java hljs"><span class="hljs-keyword">default</span> <V> BiFunction<T, U, V> andThen(Function<? <span class="hljs-keyword">super</span> R, ? extends V> after)</code></p>
<p>And defines two new <code>static</code> methods:</p>
<p><code class="java hljs"><span class="hljs-keyword">static</span> <T> <span class="hljs-function">BinaryOperator<T> <span class="hljs-title">minBy</span><span class="hljs-params">(<br />
Comparator<? <span class="hljs-keyword">super</span> T> comparator)</span><br />
<span class="hljs-keyword">static</span> <T> BinaryOperator<T> <span class="hljs-title">maxBy</span><span class="hljs-params">(<br />
Comparator<? <span class="hljs-keyword">super</span> T> comparator)</span></span></code></p>
<p>That return a <code>BinaryOperator</code>, which returns the lesser or greater of two elements according to the specified <code>Comparator</code>.</p>
<p>Here's a simple example:</p>
<p><code class="java hljs">BinaryOperator<Integer> biOp =<br />
BinaryOperator.maxBy(Comparator.naturalOrder());<br />
System.out.println(biOp.apply(<span class="hljs-number">28</span>, <span class="hljs-number">8</span>));</code></p>
<p>As you can see, these methods are just a wrapper to execute a <code>Comparator</code>.</p>
<p><code>Comparator.naturalOrder()</code> returns a <code>Comparator</code> that compares <code>Comparable</code> objects in natural order. To execute it, we just call the <code>apply()</code> method with the two arguments needed by the <code>BinaryOperator</code>. Unsurprisingly, the output is:</p>
<p><code class="java hljs"><span class="hljs-number">28</span></code></p>
<p>There are also primitive versions for <code>int</code>, <code>long</code> and <code>double</code>, where the two arguments and the return type are of the same primitive type. They don't extend from <code>BinaryOperator</code> or <code>BiFunction</code>.</p>
<p>For example, here's the definition of <code>IntBinaryOperator</code>:</p>
<p><code class="java hljs"><span class="hljs-annotation">@FunctionalInterface</span><br />
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">IntBinaryOperator</span></span> {<br />
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">applyAsInt</span><span class="hljs-params">(<span class="hljs-keyword">int</span> left, <span class="hljs-keyword">int</span> right)</span></span>;<br />
}</code></p>
<p>That you can use instead of <code>BinaryOperator</code>.</p>
<h2>Key Points</h2>
<ul>
<li>Java 8 contains new functional interfaces to work with lambda expressions that cover the most common scenarios usages located in the <code>java.util.function</code> package.<br /> They are:<br />
<ul>
<li><code>Predicate</code></li>
<li><code>Consumer</code></li>
<li><code>Function</code></li>
<li><code>Supplier</code></li>
<li><code>UnaryOperator</code></li>
</ul>
</li>
<li>These interfaces have versions that work with primitive values for <code>int</code>, <code>long</code> and <code>double</code>, and <code>boolean</code> (only for <code>Supplier</code>) just to avoid the cost of converting a wrapper class to its primitive value, for example, <code>Integer</code> to <code>int</code>.</li>
<li>These interfaces take one argument (represented by the generic type <code>T</code>), but with the exception of <code>Supplier</code> (that doesn't take any arguments), they have versions that take two arguments called binary versions.</li>
<li>A Predicate can be used anywhere you need to evaluate a <code>boolean</code> condition. Its function descriptor (method signature) is:<br />
<code class="java hljs">T -> <span class="hljs-keyword">boolean</span></code></li>
<li>It has primitive versions for <code>int</code>, <code>long</code> and <code>double</code>, for example, <code>IntPredicate</code>.</li>
<li>A Consumer is an operation that accepts a single input argument and returns no result. Its functional descriptor is:<br />
<code class="java hljs">T -> <span class="hljs-keyword">void</span></code></li>
<li>It has primitive versions for <code>int</code>, <code>long</code> and <code>double</code>, for example, <code>IntConsumer</code>.</li>
<li>A Function is an operation that takes an input argument of a certain type and produces a result of another type. Its functional descriptor is:<br />
<code class="java hljs">T -> R</code></li>
<li>It has a lot of primitive versions for <code>int</code>, <code>long</code> and <code>double</code>. We can divide them into three types.</li>
<li>To indicate that the function returns a generic type and takes a primitive argument, the interface is named <b>XXXFunction</b>, for example, <code>IntFunction</code>.</li>
<li>To indicate that the function returns a primitive type and takes a generic argument, the interface is named <b>ToXXXFunction</b>, for example, <code>ToIntFunction</code>.</li>
<li>To indicate that the function takes a primitive argument and returns another primitive type, the interface is named <b>XXXToYYYFunction</b>, where XXX is the argument type and YYY is the return type, for example, <code>IntToDoubleFunction</code>.</li>
<li>A <code>Supplier</code> is an operation that takes no arguments, but it returns some value. Its functional descriptor is:<br />
<code class="java hljs">() -> T</code></li>
<li>It has primitive versions for <code>int</code>, <code>long</code>, <code>double</code> and <code>boolean</code>, for example, <code>IntSupplier</code>.</li>
<li><code>UnaryOperator</code> is a specialization of the <code>Function</code> interface (in fact, this interface extends from it) for when the argument and the result are of the same type. So its functional descriptor is:<br />
<code class="java hljs">T -> T</code></li>
<li>It has primitive versions for <code>int</code>, <code>long</code> and <code>double</code>, for example, <code>IntUnaryOperator</code>.</li>
<li>A <code>BiPredicate</code> represents a predicate that takes two arguments. Its functional descriptor is:<br />
<code class="java hljs">(T, U) -> <span class="hljs-keyword">boolean</span></code></li>
<li>This interface doesn't have primitive versions.</li>
<li>A <code>BiConsumer</code> represents a consumer that takes two arguments. Its functional descriptor is:<br />
<code class="java hljs">(T, U) -> <span class="hljs-keyword">void</span></code></li>
<li>It has primitive versions for <code>int</code>, <code>long</code> and <code>double</code>. They take one object and a primitive value as a second argument. So the naming conventions change to <code>ObjXXXConsumer</code>, where XXX is the primitive type, for example, <code>ObjIntConsumer</code>.</li>
<li>A <code>BiFunction</code> represents a function that takes two arguments of different type and produces a result of another type. Its functional descriptor is:<br />
<code class="java hljs">(T, U) -> R</code></li>
<li>It has primitive versions that take generic types as arguments and return <code>int</code>, <code>long</code> and <code>double</code> primitive types, with the naming convention <code>ToXXXBiFunction</code>, where XXX is the primitive type.</li>
<li>A <code>BinaryOperator</code> is a specialization of the <code>BiFunction</code> interface (in fact, this interface extends from it) for when the arguments and the result are of the same type. Its functional descriptor is:<br />
<code class="java hljs">(T, T) -> T</code></li>
<li>It defines two static methods:<br />
<code class="java hljs"><span class="hljs-keyword">static</span> <T> <span class="hljs-function">BinaryOperator<T> <span class="hljs-title">minBy</span><span class="hljs-params">(<br />
Comparator<? <span class="hljs-keyword">super</span> T> comparator)<br /></span><span class="hljs-keyword">static</span> <T> BinaryOperator<T> <span class="hljs-title">maxBy</span><span class="hljs-params">(<br />
Comparator<? <span class="hljs-keyword">super</span> T> comparator)</span></span></code></li>
<li>It has primitive versions for <code>int</code>, <code>long</code> and <code>double</code>, for example, <code>IntBinaryOperator</code>.</li>
</ul>
<h2>Self Test</h2>
<p>1. Given:</p>
<p><code class="java hljs"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Question_10_1</span></span> {<br />
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span> {<br />
Predicate<String> p1 = t -> {<br />
System.out.print(<span class="hljs-string">"p1"</span>);<br />
<span class="hljs-keyword">return</span> t.startsWith(<span class="hljs-string">" "</span>);<br />
};<br />
Predicate<String> p2 = t -> {<br />
System.out.print(<span class="hljs-string">"p2"</span>);<br />
<span class="hljs-keyword">return</span> t.length() > <span class="hljs-number">5</span>;<br />
};<br />
p1.and(p2).test(<span class="hljs-string">"a question"</span>);<br />
}<br />
}</code></p>
<p>What is the result?<br /> A. <code>p1</code><br /> B. <code>p2</code><br /> C. <code>p1p2</code><br /> D. <code>false</code><br /> E. Compilation fails</p>
<p>2. Which of the following interfaces is a valid primitive version of <code>BiConsumer<T, U></code>?<br /> A. <code>IntBiConsumer</code><br /> B. <code>ObjLongConsumer</code><br /> C. <code>ToLongBiConsumer</code><br /> D. <code>IntToDoubleConsumer</code></p>
<p>3. Given:</p>
<p><code class="java hljs"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Question_10_3</span></span> {<br />
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span> {<br />
IntUnaryOperator u1 = i -> i / <span class="hljs-number">6</span>;<br />
IntUnaryOperator u2 = i -> i + <span class="hljs-number">12</span>;<br />
System.out.println(<br />
u1.compose(u2).applyAsInt(<span class="hljs-number">12</span>)<br />
);<br />
}<br />
}</code></p>
<p>What is the result?<br /> A. <code>24</code><br /> B. <code>14</code><br /> C. <code>4</code><br /> D. <code>2</code><br /> E. Compilation fails</p>
<p>4. Which of the following statements is true?<br /> A. A <code>Consumer</code> takes a parameter of type <code>T</code> and returns a result of the same type.<br /> B. <code>UnaryOperator</code> is a specialization of the <code>Operator</code> interface.<br /> C. The <code>BiFunction</code> interface doesn't have primitive versions.<br /> D. A <code>Supplier</code> represents an operation that takes no arguments, but it returns some value.</p>
<p>5. Given:</p>
<p><code class="java hljs"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Question_10_3</span></span> {<br />
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span> {<br />
Supplier<Boolean> s = () -> {<br />
Random generator = <span class="hljs-keyword">new</span> Random();<br />
<span class="hljs-keyword">int</span> n = generator.nextInt(<span class="hljs-number">1</span>);<br />
<span class="hljs-keyword">return</span> n % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>;<br />
};<br />
System.out.println(s.getAsBoolean());<br />
}<br />
}</code></p>
<p>What is the result?<br /> A. <code>true</code><br /> B. <code>false</code><br /> C. Sometimes <code>true</code>, sometimes <code>false</code><br /> D. Compilation fails</p>
<p>6. Which of the following interfaces is a valid primitive version of <code>BiPredicate<T, U></code>?<br /> A. <code>IntBiPredicate</code><br /> B. <code>ObjBooleanPredicate</code><br /> C. <code>ToLongBiPredicate</code><br /> D. <code>BiPredicate</code> doesn't have primitive versions</p>
<p>7. Which of the following primitive version of <code>Function</code> returns a generic type while taking a <code>long</code> argument?<br /> A. <code>ToLongFunction</code><br /> B. <code>LongFunction</code><br /> C. <code>LongToObjectFunction</code><br /> D. There's no primitive version with this characteristic</p>
<p>8. Which of the following statements is true?<br /> A. The <code>BinaryOperator</code> interface extends from the <code>BiFunction</code> interface.<br /> B. The <code>BiSupplier</code> interface only takes one generic argument.<br /> C. The <code>Supplier</code> interface doesn't define any default methods.<br /> D. <code>minBy</code> and <code>maxBy</code> are two default methods of the <code>BinaryOperator</code> interface.</p>
<div class="answers">
<a href="ch10a.html" target="_blank">Open answers page</a>
</div>