-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFP02Functional.java
More file actions
64 lines (50 loc) · 1.35 KB
/
FP02Functional.java
File metadata and controls
64 lines (50 loc) · 1.35 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
package programming;
import java.util.List;
import java.util.stream.Collectors;
public class FP02Functional {
public static void main(String[] args) {
List<Integer> numbers = List.of(12, 9, 13, 4, 6, 2, 4, 12, 15);
List<Integer> squaredNumbers = squareList(numbers);
List<Integer> evenNumbersOnly = numbers.stream()
.filter(x -> x%2==0)
.collect(Collectors.toList());
//System.out.println(squaredNumbers);
System.out.println(evenNumbersOnly);
// 0 12
// 12 9
// 21 13
// 34 4
// 38 6
// 44 2
// 46 4
// 50 12
// 62 15
// 77
// int sum = addListFunctional(numbers);
//
// System.out.println(sum);
}
private static List<Integer> squareList(List<Integer> numbers) {
//1 , 5, 6
//1 -> 1
//5 -> 25
//6 -> 36
return numbers.stream()
.map(number -> number * number)
.collect(Collectors.toList());
}
private static int sum(int aggregate, int nextNumber) {
System.out.println(aggregate + " " + nextNumber);
return aggregate + nextNumber;
}
private static int addListFunctional(List<Integer> numbers) {
//Stream of number -> One result value
//Combine them into one result => One Value
// 0 and FP02Functional::sum
return numbers.stream()
.parallel()
//.reduce(0, FP02Functional::sum);
// .reduce(0, (x,y) -> x + y);
.reduce(0, Integer::sum);
}
}