|
@@ -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
|
|
|
@@ -14,13 +16,15 @@ import java.util.stream.Stream;
|
14
|
16
|
*/
|
15
|
17
|
public class StreamFilter {
|
16
|
18
|
private final Stream<Person> personStream;
|
17
|
|
- public final String startingCharacter;
|
|
19
|
+ private final String startingCharacter;
|
|
20
|
+
|
18
|
21
|
|
19
|
22
|
/**
|
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
|
+
|
|
27
|
+ this(PersonFactory.createPersonStream(10), RandomUtils.createCharacter('A','Z'));
|
24
|
28
|
}
|
25
|
29
|
|
26
|
30
|
/**
|
|
@@ -28,7 +32,8 @@ public class StreamFilter {
|
28
|
32
|
* @param startingCharacter - character to filter by
|
29
|
33
|
*/ //TODO
|
30
|
34
|
public StreamFilter(Person[] people, Character startingCharacter) {
|
31
|
|
- this(Stream.empty(), null);
|
|
35
|
+
|
|
36
|
+ this(Arrays.stream(people), startingCharacter);
|
32
|
37
|
}
|
33
|
38
|
|
34
|
39
|
/**
|
|
@@ -36,7 +41,7 @@ public class StreamFilter {
|
36
|
41
|
* @param startingCharacter - character to filter by
|
37
|
42
|
*/ //TODO
|
38
|
43
|
public StreamFilter(List<Person> people, Character startingCharacter) {
|
39
|
|
- this(Stream.empty(), null);
|
|
44
|
+ this(people.stream(), startingCharacter);
|
40
|
45
|
}
|
41
|
46
|
|
42
|
47
|
|
|
@@ -44,6 +49,7 @@ public class StreamFilter {
|
44
|
49
|
* @param people - Stream of person objects
|
45
|
50
|
* @param startingCharacter - character to filter by
|
46
|
51
|
*/ // I took care of the easy constructor (͡° ͜ʖ ͡°)
|
|
52
|
+
|
47
|
53
|
public StreamFilter(Stream<Person> people, Character startingCharacter) {
|
48
|
54
|
this.personStream = people;
|
49
|
55
|
this.startingCharacter = startingCharacter.toString();
|
|
@@ -55,7 +61,12 @@ public class StreamFilter {
|
55
|
61
|
* @return a list of person object whose name starts with `this.startingCharacter`
|
56
|
62
|
*/ //TODO
|
57
|
63
|
public List<Person> toListMultiLine() {
|
58
|
|
- return null;
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+ Predicate<Person> filter = person -> StringUtils.isPalindromeIgnoreCase(person.getName());
|
|
67
|
+ return personStream
|
|
68
|
+ .filter(filter)
|
|
69
|
+ .collect(Collectors.toList());
|
59
|
70
|
}
|
60
|
71
|
|
61
|
72
|
|
|
@@ -64,7 +75,7 @@ public class StreamFilter {
|
64
|
75
|
* @return a list of person objects whose name starts with `this.startingCharacter`
|
65
|
76
|
*/ //TODO
|
66
|
77
|
public List<Person> toListOneLine() {
|
67
|
|
- return null;
|
|
78
|
+ return personStream.filter(person -> StringUtils.isPalindromeIgnoreCase(person.getName())).collect(Collectors.toList());
|
68
|
79
|
}
|
69
|
80
|
|
70
|
81
|
|
|
@@ -73,7 +84,7 @@ public class StreamFilter {
|
73
|
84
|
* @return an array of person object whose name starts with `this.startingCharacter`
|
74
|
85
|
*/ //TODO
|
75
|
86
|
public Person[] toArrayOneLine() {
|
76
|
|
- return null;
|
|
87
|
+ return personStream.filter(person -> StringUtils.isPalindromeIgnoreCase(person.getName())).toArray(Person[]::new);
|
77
|
88
|
}
|
78
|
89
|
|
79
|
90
|
|
|
@@ -82,7 +93,11 @@ public class StreamFilter {
|
82
|
93
|
* @return an array of person object whose name starts with `this.startingCharacter`
|
83
|
94
|
*/ //TODO
|
84
|
95
|
public Person[] toArrayMultiLine() {
|
85
|
|
- return null;
|
|
96
|
+ return personStream.filter(person -> {StringBuilder reverseName = new StringBuilder();
|
|
97
|
+ for (int i = person.getName().length() - 1; i > 0; i--) {
|
|
98
|
+ reverseName.append(person.getName().charAt(i));
|
|
99
|
+ } return reverseName.toString().equalsIgnoreCase(person.getName());
|
|
100
|
+ }).toArray(Person[]::new);
|
86
|
101
|
}
|
87
|
102
|
|
88
|
103
|
}
|