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