1
1
package java8tutorials .functionalInterfaces ;
2
2
3
- /**
4
- * Created by stevesun on 4/16/17.
5
- */
3
+ import java .util .function .Function ;
4
+
6
5
public class FunctionalInterfacesDemo {
7
- /**How does lambda expressions fit into Javas type system?
6
+ /**
7
+ * How does lambda expressions fit into Javas type system?
8
8
* Each lambda corresponds to a given type, specified by an interface.
9
9
* A so called functional interface must contain exactly one abstract method declaration.
10
10
* Each lambda expression of that type will be matched to this abstract method.
@@ -13,9 +13,10 @@ public class FunctionalInterfacesDemo {
13
13
* To ensure that your interface meet the requirements,
14
14
* you should add the @FunctionalInterface annotation.
15
15
* The compiler is aware of this annotation and throws a compiler error as soon as you try to add a second abstract method declaration to
16
- * the interface.*/
16
+ * the interface.
17
+ */
17
18
18
- public static void main (String ... args ) {
19
+ public static void main (String ... args ) {
19
20
Converter <String , Integer > converter = (from -> Integer .valueOf (from ));
20
21
Integer converted = converter .convert ("123" );
21
22
System .out .println (converted );
@@ -25,6 +26,72 @@ public static void main(String ... args) {
25
26
converter = Integer ::valueOf ;
26
27
converted = converter .convert ("321" );
27
28
System .out .println (converted );
29
+
30
+ runAnotherFunctionInterfaceExample ();
31
+ }
32
+
33
+ private static void runAnotherFunctionInterfaceExample () {
34
+ /**Function interface has a few methods that are often used:
35
+ * apply()
36
+ * andThen()
37
+ * compose()
38
+ * identity()
39
+ * */
40
+ Function <Integer , Integer > addFunction = a -> a + 3 ;
41
+ System .out .println (addFunction .apply (1 ));
42
+
43
+ Function <Integer , Integer > multipleFunction = (a ) -> a * 3 ;
44
+ System .out .println (multipleFunction .apply (1 ));
45
+
46
+ //a.compose(b) means b will be executed first and then a will execute
47
+ Function <Integer , Integer > compositeFunction = addFunction .compose (multipleFunction );
48
+ System .out .println (compositeFunction .apply (1 ));
49
+
50
+ //a.andThen(b) means a will be executed first, and then function b executes.
51
+ Function <Integer , Integer > andThenFunction = addFunction .andThen (multipleFunction );
52
+ System .out .println (andThenFunction .apply (1 ));
53
+
54
+ //Function.identity() is a static method of Function interface that returns a Function that always returns its input argument. i.e. f(x) = x
55
+ understandFunctionIdentity ();
56
+ }
57
+
58
+ private static void understandFunctionIdentity () {
59
+ // Using String as Input for Function.identity()
60
+ Function <String , String > stringFunction = Function .identity ();
61
+ System .out .println (stringFunction .apply ("Alive is Awesome" ));
62
+
63
+ // Using Integer as input for Function.identity()
64
+ Function <Integer , Integer > integerFunctionUsingFunctionIdentity = Function .identity ();
65
+ System .out .println (integerFunctionUsingFunctionIdentity .apply (8 ));
66
+
67
+ // Using lambda expression and String as input
68
+ Function <String , String > stringFunctionUsingLambda = t -> t ;
69
+ System .out .println (stringFunctionUsingLambda .apply ("Be in present" ));
70
+
71
+ // Using lambda expression and Integer as input
72
+ Function <Integer , Integer > integerFunctionUsingLambda = t -> t ;
73
+ System .out .println (integerFunctionUsingLambda .apply (4 ));
74
+
75
+ Function <Integer , Integer > func1 = Function .identity ();
76
+ Function <Integer , Integer > func2 = Function .identity ();
77
+ Function <Integer , Integer > func3 = Function .identity ();
78
+
79
+ Function <Integer , Integer > intFunc1 = t -> t ;
80
+ Function <Integer , Integer > intFunc2 = t -> t ;
81
+ Function <Integer , Integer > intFunc3 = t -> t ;
82
+
83
+ System .out .println (func1 );
84
+ System .out .println (func2 );
85
+ System .out .println (func3 );
86
+
87
+ System .out .println (intFunc1 );
88
+ System .out .println (intFunc2 );
89
+ System .out .println (intFunc3 );
90
+ /**
91
+ * From the above output, we can conclude that Function.identity()
92
+ * method will always return the same instance
93
+ * whereas each occurrence of (t -> t) or identifier -> identifier
94
+ * will not on
41AC
ly create its own instance but even have a distinct implementation class.*/
28
95
}
29
96
30
97
}
0 commit comments