-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipeAndFilter.java
More file actions
61 lines (51 loc) · 2.11 KB
/
Copy pathPipeAndFilter.java
File metadata and controls
61 lines (51 loc) · 2.11 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
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class PipeAndFilter {
public static void main(String[] args) {
List<Integer> input = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Create a pipeline
List<Function<List<Integer>, List<Integer>>> filters = new ArrayList<>();
filters.add(PipeAndFilter::filterEvenNumbers);
filters.add(PipeAndFilter::squareNumbers);
filters.add(PipeAndFilter::filterNumbersGreaterThanTen);
filters.add(PipeAndFilter::filterDivisibleByThree); // New filter added
// Process the input through the pipeline
List<Integer> result = processPipeline(input, filters);
// Output the result
System.out.println(result);
}
// Process the input through the pipeline of filters
private static List<Integer> processPipeline(List<Integer> input, List<Function<List<Integer>, List<Integer>>> filters) {
List<Integer> output = input;
for (Function<List<Integer>, List<Integer>> filter : filters) {
output = filter.apply(output);
}
return output;
}
// Filter to keep even numbers
private static List<Integer> filterEvenNumbers(List<Integer> input) {
return input.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
}
// Filter to square the numbers
private static List<Integer> squareNumbers(List<Integer> input) {
return input.stream()
.map(n -> n * n)
.collect(Collectors.toList());
}
// Filter to keep numbers greater than 10
private static List<Integer> filterNumbersGreaterThanTen(List<Integer> input) {
return input.stream()
.filter(n -> n > 10)
.collect(Collectors.toList());
}
// New filter to keep numbers divisible by 3
private static List<Integer> filterDivisibleByThree(List<Integer> input) {
return input.stream()
.filter(n -> n % 3 == 0)
.collect(Collectors.toList());
}
}