|
@@ -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.Random;
|
9
|
11
|
import java.util.stream.Collectors;
|
10
|
12
|
import java.util.stream.Stream;
|
11
|
13
|
|
|
@@ -20,7 +22,7 @@ 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(PersonFactory.createPersonStream(10), RandomUtils.createCharacter('A','Z'));
|
24
|
26
|
}
|
25
|
27
|
|
26
|
28
|
|
|
@@ -29,7 +31,7 @@ public class StreamFilter {
|
29
|
31
|
* @param startingCharacter - character to filter by
|
30
|
32
|
*/ //TODO
|
31
|
33
|
public StreamFilter(Person[] people, Character startingCharacter) {
|
32
|
|
- this(Stream.empty(), null);
|
|
34
|
+ this(Arrays.stream(people), startingCharacter);
|
33
|
35
|
}
|
34
|
36
|
|
35
|
37
|
/**
|
|
@@ -37,7 +39,7 @@ public class StreamFilter {
|
37
|
39
|
* @param startingCharacter - character to filter by
|
38
|
40
|
*/ //TODO
|
39
|
41
|
public StreamFilter(List<Person> people, Character startingCharacter) {
|
40
|
|
- this(Stream.empty(), null);
|
|
42
|
+ this(people.stream(),startingCharacter);
|
41
|
43
|
}
|
42
|
44
|
|
43
|
45
|
|
|
@@ -56,7 +58,11 @@ public class StreamFilter {
|
56
|
58
|
* @return a list of person object whose name starts with `this.startingCharacter`
|
57
|
59
|
*/ //TODO
|
58
|
60
|
public List<Person> toListMultiLine() {
|
59
|
|
- return null;
|
|
61
|
+ return personStream.filter(person -> {StringBuilder reverseName = new StringBuilder();
|
|
62
|
+ for (int i = person.getName().length() - 1; i > 0; i--) {
|
|
63
|
+ reverseName.append(person.getName().charAt(i));
|
|
64
|
+ } return reverseName.toString().equalsIgnoreCase(person.getName());
|
|
65
|
+ }).collect(Collectors.toList());
|
60
|
66
|
}
|
61
|
67
|
|
62
|
68
|
|
|
@@ -65,7 +71,7 @@ public class StreamFilter {
|
65
|
71
|
* @return a list of person objects whose name starts with `this.startingCharacter`
|
66
|
72
|
*/ //TODO
|
67
|
73
|
public List<Person> toListOneLine() {
|
68
|
|
- return null;
|
|
74
|
+ return personStream.filter(person -> StringUtils.isPalindromeIgnoreCase(person.getName())).collect(Collectors.toList());
|
69
|
75
|
}
|
70
|
76
|
|
71
|
77
|
|
|
@@ -74,7 +80,7 @@ public class StreamFilter {
|
74
|
80
|
* @return an array of person object whose name starts with `this.startingCharacter`
|
75
|
81
|
*/ //TODO
|
76
|
82
|
public Person[] toArrayOneLine() {
|
77
|
|
- return null;
|
|
83
|
+ return personStream.filter(person -> StringUtils.isPalindromeIgnoreCase(person.getName())).toArray(Person[]::new);
|
78
|
84
|
}
|
79
|
85
|
|
80
|
86
|
|
|
@@ -83,7 +89,11 @@ public class StreamFilter {
|
83
|
89
|
* @return an array of person object whose name starts with `this.startingCharacter`
|
84
|
90
|
*/ //TODO
|
85
|
91
|
public Person[] toArrayMultiLine() {
|
86
|
|
- return null;
|
|
92
|
+ return personStream.filter(person -> {StringBuilder reverseName = new StringBuilder();
|
|
93
|
+ for (int i = person.getName().length() - 1; i > 0; i--) {
|
|
94
|
+ reverseName.append(person.getName().charAt(i));
|
|
95
|
+ } return reverseName.toString().equalsIgnoreCase(person.getName());
|
|
96
|
+ }).toArray(Person[]::new);
|
87
|
97
|
}
|
88
|
98
|
|
89
|
99
|
}
|