-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava8FunctionInterfaceExample.java
More file actions
40 lines (34 loc) · 1.34 KB
/
Copy pathJava8FunctionInterfaceExample.java
File metadata and controls
40 lines (34 loc) · 1.34 KB
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
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
/**
* Created by john on 2016/1/7.
*/
public class Java8FunctionInterfaceExample {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9);
//Predicate<Integer> predicate = n -> true
// n is passed as parameter to test method of Predicate interface
// test method will always return true what value n has
System.out.println("Print all number");
eval(list,n->true);
// Predicate<Integer> predicate1 = n -> n%2 == 0
// n is passed as parameter to test method of Predicate interface
// test method will return true if n%2 comes to be zero
System.out.println("Print even numbers:");
eval(list, n-> n%2 == 0 );
// Predicate<Integer> predicate2 = n -> n > 3
// n is passed as parameter to test method of Predicate interface
// test method will return true if n is greater than 3.
System.out.println("Print numbers greater than 3:");
eval(list, n-> n > 3 );
}
public static void eval(List<Integer> list, Predicate<Integer> predicate) {
for(Integer n : list) {
if(predicate.test(n)) {
System.out.print(n + " ");
}
}
System.out.println();
}
}