8000 Function interface examples · fishercoder1534/RandomJava@d4ab0c2 · GitHub
[go: up one dir, main page]

Skip to content

Commit d4ab0c2

Browse files
Function interface examples
1 parent 3e3882a commit d4ab0c2

File tree

1 file changed

+73
-6
lines changed

1 file changed

+73
-6
lines changed
Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package java8tutorials.functionalInterfaces;
22

3-
/**
4-
* Created by stevesun on 4/16/17.
5-
*/
3+
import java.util.function.Function;
4+
65
public class FunctionalInterfacesDemo {
7-
/**How does lambda expressions fit into Javas type system?
6+
/**
7+
* How does lambda expressions fit into Javas type system?
88
* Each lambda corresponds to a given type, specified by an interface.
99
* A so called functional interface must contain exactly one abstract method declaration.
1010
* Each lambda expression of that type will be matched to this abstract method.
@@ -13,9 +13,10 @@ public class FunctionalInterfacesDemo {
1313
* To ensure that your interface meet the requirements,
1414
* you should add the @FunctionalInterface annotation.
1515
* 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+
*/
1718

18-
public static void main(String ... args) {
19+
public static void main(String... args) {
1920
Converter<String, Integer> converter = (from -> Integer.valueOf(from));
2021
Integer converted = converter.convert("123");
2122
System.out.println(converted);
@@ -25,6 +26,72 @@ public static void main(String ... args) {
2526
converter = Integer::valueOf;
2627
converted = converter.convert("321");
2728
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.*/
2895
}
2996

3097
}

0 commit comments

Comments
 (0)
0