|
@@ -19,7 +19,7 @@ public class StreamFilter {
|
19
|
19
|
|
20
|
20
|
/**
|
21
|
21
|
* No arg constructor
|
22
|
|
- */ //TODO - construct person stream of 100 person objects; startingCharacter is a random capital letter
|
|
22
|
+ */
|
23
|
23
|
public StreamFilter() {
|
24
|
24
|
this(PersonFactory.createPersonStream(100), RandomUtils.createCharacter('A', 'Z'));
|
25
|
25
|
}
|
|
@@ -35,7 +35,7 @@ public class StreamFilter {
|
35
|
35
|
/**
|
36
|
36
|
* @param people - List of person objects
|
37
|
37
|
* @param startingCharacter - character to filter by
|
38
|
|
- */ //TODO
|
|
38
|
+ */
|
39
|
39
|
public StreamFilter(List<Person> people, Character startingCharacter) {
|
40
|
40
|
this(people.stream(), startingCharacter);
|
41
|
41
|
}
|
|
@@ -54,36 +54,45 @@ public class StreamFilter {
|
54
|
54
|
/**
|
55
|
55
|
* Using multi-line lambda syntax
|
56
|
56
|
* @return a list of person object whose name starts with `this.startingCharacter`
|
57
|
|
- */ //TODO
|
|
57
|
+ */ // Is this what he means?
|
58
|
58
|
public List<Person> toListMultiLine() {
|
59
|
|
- return null;
|
|
59
|
+ return personStream.filter(p -> {
|
|
60
|
+ return p.getName().charAt(0) == startingCharacter.charAt(0);
|
|
61
|
+ }).collect(Collectors.toList());
|
60
|
62
|
}
|
61
|
63
|
|
62
|
64
|
|
63
|
65
|
/**
|
64
|
66
|
* Using one-line lambda syntax
|
65
|
67
|
* @return a list of person objects whose name starts with `this.startingCharacter`
|
66
|
|
- */ //TODO
|
|
68
|
+ */
|
67
|
69
|
public List<Person> toListOneLine() {
|
68
|
|
- return null;
|
|
70
|
+ return personStream
|
|
71
|
+ .filter(p -> p.getName().charAt(0) == startingCharacter.charAt(0))
|
|
72
|
+ .collect(Collectors.toList());
|
69
|
73
|
}
|
70
|
74
|
|
71
|
75
|
|
72
|
76
|
/**
|
73
|
77
|
* Using one-line lambda syntax
|
74
|
78
|
* @return an array of person object whose name starts with `this.startingCharacter`
|
75
|
|
- */ //TODO
|
|
79
|
+ */ //
|
76
|
80
|
public Person[] toArrayOneLine() {
|
77
|
|
- return null;
|
|
81
|
+ return personStream
|
|
82
|
+ .filter(p -> p.getName().charAt(0) == startingCharacter.charAt(0))
|
|
83
|
+ .toArray(Person[]::new);
|
78
|
84
|
}
|
79
|
85
|
|
80
|
86
|
|
81
|
87
|
/**
|
82
|
88
|
* Using multi-line lambda syntax
|
83
|
89
|
* @return an array of person object whose name starts with `this.startingCharacter`
|
84
|
|
- */ //TODO
|
|
90
|
+ */ // again, not sure if this is what he means
|
85
|
91
|
public Person[] toArrayMultiLine() {
|
86
|
|
- return null;
|
|
92
|
+ return personStream
|
|
93
|
+ .filter(p -> {
|
|
94
|
+ return p.getName().charAt(0) == startingCharacter.charAt(0);
|
|
95
|
+ }).toArray(Person[]::new);
|
87
|
96
|
}
|
88
|
97
|
|
89
|
98
|
}
|