|
@@ -5,7 +5,9 @@ import com.zipcodewilmington.streams.anthropoid.PersonFactory;
|
5
|
5
|
import com.zipcodewilmington.streams.tools.RandomUtils;
|
6
|
6
|
import com.zipcodewilmington.streams.tools.StringUtils;
|
7
|
7
|
|
|
8
|
+import java.util.Arrays;
|
8
|
9
|
import java.util.List;
|
|
10
|
+import java.util.function.Predicate;
|
9
|
11
|
import java.util.stream.Collectors;
|
10
|
12
|
import java.util.stream.Stream;
|
11
|
13
|
|
|
@@ -20,7 +22,8 @@ public class StreamFilter {
|
20
|
22
|
* No arg constructor
|
21
|
23
|
*/ //TODO - construct person stream of 100 person objects; startingCharacter is a random capital letter
|
22
|
24
|
public StreamFilter() {
|
23
|
|
- this(Stream.empty(), null);
|
|
25
|
+ this.startingCharacter = String.valueOf((char)(Math.random() * (90 - 65)) + 65);
|
|
26
|
+ this.personStream = PersonFactory.createPersonStream(100);
|
24
|
27
|
}
|
25
|
28
|
|
26
|
29
|
/**
|
|
@@ -28,7 +31,8 @@ public class StreamFilter {
|
28
|
31
|
* @param startingCharacter - character to filter by
|
29
|
32
|
*/ //TODO
|
30
|
33
|
public StreamFilter(Person[] people, Character startingCharacter) {
|
31
|
|
- this(Stream.empty(), null);
|
|
34
|
+ this.startingCharacter = String.valueOf(startingCharacter);
|
|
35
|
+ this.personStream = Arrays.stream(people);
|
32
|
36
|
}
|
33
|
37
|
|
34
|
38
|
/**
|
|
@@ -36,7 +40,8 @@ public class StreamFilter {
|
36
|
40
|
* @param startingCharacter - character to filter by
|
37
|
41
|
*/ //TODO
|
38
|
42
|
public StreamFilter(List<Person> people, Character startingCharacter) {
|
39
|
|
- this(Stream.empty(), null);
|
|
43
|
+ this.startingCharacter = String.valueOf(startingCharacter);
|
|
44
|
+ this.personStream = people.stream();
|
40
|
45
|
}
|
41
|
46
|
|
42
|
47
|
|
|
@@ -55,7 +60,8 @@ public class StreamFilter {
|
55
|
60
|
* @return a list of person object whose name starts with `this.startingCharacter`
|
56
|
61
|
*/ //TODO
|
57
|
62
|
public List<Person> toListMultiLine() {
|
58
|
|
- return null;
|
|
63
|
+ Predicate<Person> startsWithStartingCharacter = p -> p.getName().matches("^" + this.startingCharacter);
|
|
64
|
+ return personStream.filter(startsWithStartingCharacter).collect(Collectors.toList());
|
59
|
65
|
}
|
60
|
66
|
|
61
|
67
|
|
|
@@ -64,7 +70,7 @@ public class StreamFilter {
|
64
|
70
|
* @return a list of person objects whose name starts with `this.startingCharacter`
|
65
|
71
|
*/ //TODO
|
66
|
72
|
public List<Person> toListOneLine() {
|
67
|
|
- return null;
|
|
73
|
+ return personStream.filter(p -> p.getName().matches("^" + this.startingCharacter)).collect(Collectors.toList());
|
68
|
74
|
}
|
69
|
75
|
|
70
|
76
|
|
|
@@ -73,7 +79,7 @@ public class StreamFilter {
|
73
|
79
|
* @return an array of person object whose name starts with `this.startingCharacter`
|
74
|
80
|
*/ //TODO
|
75
|
81
|
public Person[] toArrayOneLine() {
|
76
|
|
- return null;
|
|
82
|
+ return personStream.filter(p -> p.getName().matches("^" + this.startingCharacter)).toArray(Person[]::new);
|
77
|
83
|
}
|
78
|
84
|
|
79
|
85
|
|
|
@@ -82,7 +88,8 @@ public class StreamFilter {
|
82
|
88
|
* @return an array of person object whose name starts with `this.startingCharacter`
|
83
|
89
|
*/ //TODO
|
84
|
90
|
public Person[] toArrayMultiLine() {
|
85
|
|
- return null;
|
|
91
|
+ Predicate<Person> startsWithStartingCharacter = p -> p.getName().matches("^" + this.startingCharacter);
|
|
92
|
+ return personStream.filter(startsWithStartingCharacter).toArray(Person[]::new);
|
86
|
93
|
}
|
87
|
94
|
|
88
|
95
|
}
|