-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFP03BehaviorParameterization.java
More file actions
45 lines (30 loc) · 1.18 KB
/
FP03BehaviorParameterization.java
File metadata and controls
45 lines (30 loc) · 1.18 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
package programming;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class FP03BehaviorParameterization {
@SuppressWarnings("unused")
public static void main(String[] args) {
List<Integer> numbers = List.of(12, 9, 13, 4, 6, 2, 4, 12, 15);
//filterAndPrint(numbers, x -> x%2==0);
//filterAndPrint(numbers, x -> x%2!=0);
filterAndPrint(numbers, x -> x%3==0);
Function<Integer, Integer> mappingFunction = x -> x*x;
List<Integer> squaredNumbers = mapAndCreateNewList(numbers, mappingFunction);
List<Integer> cubedNumbers = mapAndCreateNewList(numbers, x -> x*x*x);
List<Integer> doubledNumbers = mapAndCreateNewList(numbers, x -> x + x);
System.out.println(doubledNumbers);
}
private static List<Integer> mapAndCreateNewList(List<Integer> numbers,
Function<Integer, Integer> mappingFunction) {
return numbers.stream()
.map(mappingFunction)
.collect(Collectors.toList());
}
private static void filterAndPrint(List<Integer> numbers, Predicate<? super Integer> predicate) {
numbers.stream()
.filter(predicate)
.forEach(System.out::println);
}
}