File tree Expand file tree Collapse file tree 3 files changed +48
-0
lines changed
Modern-Java-Examples/src/com/learn/functionalInterfaces Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .learn .functionalInterfaces ;
2
+
3
+ import java .util .Comparator ;
4
+ import java .util .function .BinaryOperator ;
5
+
6
+ /**
7
+ * <p>
8
+ * BinaryOperator extends BiFunction Functional Interface.
9
+ * Use BinaryOperator, when inputs and output are of same type.
10
+ * </p>
11
+ */
12
+ public class BinaryOperatorExample {
13
+
14
+ static Comparator <Integer > comparator = (a , b ) -> a .compareTo (b );
15
+
16
+
17
+ public static void main (String [] args ) {
18
+
19
+ BinaryOperator <Integer > binaryOperator = (num1 , num2 ) -> num1 * num2 ;
20
+ System .out .println (binaryOperator .apply (10 , 20 ));
21
+
22
+ BinaryOperator <Integer > maxBy = BinaryOperator .maxBy (comparator );
23
+ System .out .println ("Maximum = " + maxBy .apply (10 , 20 ));
24
+
25
+ BinaryOperator <Integer > minBy = BinaryOperator .minBy (comparator );
26
+ System .out .println ("Minimum = " + minBy .apply (10 , 20 ));
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ package com .learn .functionalInterfaces ;
2
+
3
+ import java .util .function .UnaryOperator ;
4
+
5
+ /**
6
+ * <p>
7
+ * Use UnaryOperator when input and output are of
8
+ * same type.
9
+ * </p>
10
+ */
11
+ public class UnaryOperatorExample {
12
+
13
+ static UnaryOperator <String > unaryOperator = String ::toUpperCase ;
14
+
15
+ public static void main (String [] args ) {
16
+ System .out .println (unaryOperator .apply ("test" ));
17
+ }
18
+ }
Original file line number Diff line number Diff line change @@ -13,6 +13,8 @@ This repository contains the basic & advance level examples related to Java
13
13
* [ BiPredicate] ( Modern-Java-Examples/src/com/learn/functionalInterfaces/BiPredicateExample.java )
14
14
* [ Function] ( Modern-Java-Examples/src/com/learn/functionalInterfaces/FunctionExample.java )
15
15
* [ BiFunction] ( Modern-Java-Examples/src/com/learn/functionalInterfaces/BiFunctionExample.java )
16
+ * [ UnaryOperator] ( Modern-Java-Examples/src/com/learn/functionalInterfaces/UnaryOperatorExample.java )
17
+ * [ BinaryOperator] ( Modern-Java-Examples/src/com/learn/functionalInterfaces/BinaryOperatorExample.java )
16
18
17
19
18
20
You can’t perform that action at this time.
0 commit comments