-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFP03FunctionalInterfaces.java
More file actions
80 lines (56 loc) · 1.65 KB
/
FP03FunctionalInterfaces.java
File metadata and controls
80 lines (56 loc) · 1.65 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
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
package programming;
import java.util.List;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class FP03FunctionalInterfaces {
/*
boolean isEven(int x) {
return x%2==0;
}
int squared(int x) {
return x * x;
}
*/
@SuppressWarnings("unused")
public static void main(String[] args) {
List<Integer> numbers = List.of(12, 9, 13, 4, 6, 2, 4, 12, 15);
Predicate<Integer> isEvenPredicate = x -> x%2==0;
Predicate<Integer> isEvenPredicate2 = new Predicate<Integer>() {
@Override
public boolean test(Integer x) {
return x%2==0;
}
};
Function<Integer, Integer> squareFunction = x -> x * x;
Function<Integer, Integer> squareFunction2 = new Function<Integer, Integer>() {
@Override
public Integer apply(Integer x) {
return x*x;
}
};
Consumer<Integer> sysoutConsumer = System.out::println;
Consumer<Integer> sysoutConsumer2 = new Consumer<Integer>() {
public void accept(Integer x) {
System.out.println(x);
}
};
numbers.stream()
.filter(isEvenPredicate2)
.map(squareFunction2)
.forEach(sysoutConsumer2);
BinaryOperator<Integer> sumBinaryOperator = Integer::sum;
//BinaryOperator<Integer> sumBinaryOperator = (x,y) => x + y;
BinaryOperator<Integer> sumBinaryOperator2 = new BinaryOperator<Integer>() {
@Override
public Integer apply(Integer a, Integer b) {
// TODO Auto-generated method stub
return a + b;
}
};
int sum = numbers.stream()
.reduce(0, sumBinaryOperator);
}
}