-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamFilterDemo.java
More file actions
128 lines (114 loc) · 5.12 KB
/
StreamFilterDemo.java
File metadata and controls
128 lines (114 loc) · 5.12 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
public class Main {
static Parent parent = new Parent("Parent 01");
public static void main(String[] args) {
initData();
// If parent has daughters who ONLY have sons (no daughters) that are aged 40 and above.
boolean fortyYrOldGrandKids = parent.getChildren()
.stream()
.filter(c -> "F".equals(c.getGender()) && !c.getChildren().isEmpty())
.anyMatch(c ->
c.getChildren()
.stream()
.allMatch(g -> "M".equals(g.getGender()) && g.getAge() > 40));
System.out.println("fortyYrOldGrandKids Matched? " + fortyYrOldGrandKids);
// v2
long countFortyYrOldGrandKids = parent.getChildren()
.stream()
.filter(c -> "F".equals(c.getGender()) && !c.getChildren().isEmpty() &&
c.getChildren()
.stream()
.allMatch(g -> "M".equals(g.getGender()) && g.getAge() > 40))
.count();
System.out.println("countFortyYrOldGrandKids Matched? " + countFortyYrOldGrandKids);
// Who are these children?
parent.getChildren()
.stream()
.filter(c -> "F".equals(c.getGender()) && !c.getChildren().isEmpty() &&
c.getChildren()
.stream()
.allMatch(g -> "M".equals(g.getGender()) && g.getAge() > 40))
.forEach(c -> System.out.println(c.getName()));
// If parent has sons who only have daughters that are aged 40 and below.
boolean ltFortyYrOldGrandKids = parent.getChildren()
.stream()
.filter(c -> "M".equals(c.getGender()) && !c.getChildren().isEmpty())
.anyMatch(c -> c.getChildren()
.stream()
.allMatch(g -> "F".equals(g.getGender()) && g.getAge() <= 40));
System.out.println("ltFortyYrOldGrandKids Matched? " + ltFortyYrOldGrandKids);
parent.getChildren()
.stream()
.filter(c -> "M".equals(c.getGender()) && !c.getChildren().isEmpty() &&
c.getChildren()
.stream()
.allMatch(g -> "F".equals(g.getGender()) && g.getAge() < 40))
.forEach(c -> System.out.println(c.getName()));
// Return a list if parent has sons who only have daughters that are aged 40 and below.
List<Child> children = parent.getChildren()
.stream()
.filter(c -> "M".equals(c.getGender()) && !c.getChildren()
.isEmpty() &&
c.getChildren()
.stream()
.allMatch(g -> "F".equals(g.getGender()) && g.getAge() < 40))
.collect(Collectors.toUnmodifiableList());
children.forEach(c -> System.out.println(c.getName()));
// Number of childless children.
long childless = parent.getChildren()
.stream()
.filter(c -> c.getChildren()
.isEmpty())
.count();
System.out.println("childless count? " + childless);
}
private static void initData() {
Child child = new Child("Child 01", "F", 85);
Child grandChild = new Child("Child 011", "M", 50);
child.getChildren()
.add(grandChild);
grandChild = new Child("Child 012", "M", 46);
child.getChildren()
.add(grandChild);
grandChild = new Child("Child 013", "M", 47);
child.getChildren()
.add(grandChild);
grandChild = new Child("Child 014", "M", 49);
child.getChildren()
.add(grandChild);
parent.getChildren()
.add(child);
child = new Child("Child 02", "M", 75);
grandChild = new Child("Child 021", "F", 40);
child.getChildren()
.add(grandChild);
grandChild = new Child("Child 022", "F", 38);
child.getChildren()
.add(grandChild);
grandChild = new Child("Child 023", "F", 25);
child.getChildren()
.add(grandChild);
parent.getChildren()
.add(child);
child = new Child("Child 03", "M", 30);
parent.getChildren()
.add(child);
child = new Child("Child 04", "F", 31);
parent.getChildren()
.add(child);
child = new Child("Child 05", "F", 90);
grandChild = new Child("Child 051", "M", 60);
child.getChildren()
.add(grandChild);
grandChild = new Child("Child 052", "F", 56);
child.getChildren()
.add(grandChild);
grandChild = new Child("Child 053", "M", 29);
child.getChildren()
.add(grandChild);
grandChild = new Child("Child 054", "M", 25);
child.getChildren()
.add(grandChild);
parent.getChildren()
.add(child);
}
}