Jevit Tith 6 yıl önce
ebeveyn
işleme
9c0b577f9b

+ 17
- 7
src/main/java/com/zipcodewilmington/streams/StreamFilter.java Dosyayı Görüntüle

@@ -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
 }

+ 4
- 3
src/main/java/com/zipcodewilmington/streams/StreamMap.java Dosyayı Görüntüle

@@ -18,7 +18,8 @@ public class StreamMap {
18 18
      * @return - a Stream of single characters
19 19
      */ //TODO
20 20
     public static Stream<String> letters(String someWord) {
21
-        return null;
21
+        String[] characters = someWord.split("");
22
+        return Arrays.stream(characters);
22 23
     }
23 24
 
24 25
     /**
@@ -26,7 +27,7 @@ public class StreamMap {
26 27
      * @return - a Stream of several Streams of single characters
27 28
      */ //TODO
28 29
     public static Stream<Stream<String>> wordsMap(String... someWords) {
29
-        return null;
30
+        return Stream.of(someWords).map(word -> letters(word));
30 31
     }
31 32
 
32 33
     /**
@@ -34,6 +35,6 @@ public class StreamMap {
34 35
      * @return - a Stream of several Streams of single characters
35 36
      */ //TODO
36 37
     public static Stream<String> wordsFlatMap(String... stringArray) {
37
-        return null;
38
+        return Stream.of(stringArray).flatMap(word -> letters(word));
38 39
     }
39 40
 }

+ 8
- 6
src/main/java/com/zipcodewilmington/streams/anthropoid/PersonFactory.java Dosyayı Görüntüle

@@ -38,8 +38,9 @@ public final class PersonFactory {
38 38
      * @param listSize - number of Person objects to create
39 39
      * @return - ArrayList of Person objects
40 40
      */ // TODO
41
-    public List<Person> createPersonList(int listSize) {
42
-        return null;
41
+    public static List<Person> createPersonList(int listSize) {
42
+        Stream<Person> personStream = Stream.generate(PersonFactory::createRandomPerson).limit(listSize);
43
+        return personStream.collect(Collectors.toList());
43 44
     }
44 45
 
45 46
 
@@ -47,8 +48,9 @@ public final class PersonFactory {
47 48
      * @param arrayLength - number of Person objects to create
48 49
      * @return - Array of Person objects
49 50
      */ // TODO
50
-    public Person[] createPersonArray(int arrayLength) {
51
-        return null;
51
+    public static Person[] createPersonArray(int arrayLength) {
52
+        Stream<Person> personStream = Stream.generate(PersonFactory::createRandomPerson).limit(arrayLength);
53
+        return personStream.toArray(Person[]::new);
52 54
     }
53 55
 
54 56
 
@@ -58,7 +60,7 @@ public final class PersonFactory {
58 60
      * @param streamCount - number of Person objects to create
59 61
      * @return - Stream representation of collection of Person objects
60 62
      */ // TODO
61
-    public Stream<Person> createPersonStream(int streamCount) {
62
-        return null;
63
+    public static Stream<Person> createPersonStream(int streamCount) {
64
+        return Stream.generate(PersonFactory::createRandomPerson).limit(streamCount);
63 65
     }
64 66
 }

+ 5
- 5
src/main/java/com/zipcodewilmington/streams/anthropoid/PersonWarehouse.java Dosyayı Görüntüle

@@ -36,7 +36,7 @@ public final class PersonWarehouse implements Iterable<Person> {
36 36
      * @return list of names of Person objects
37 37
      */ // TODO
38 38
     public List<String> getNames() {
39
-        return null;
39
+        return people.stream().map(Person::getName).collect(Collectors.toList());
40 40
     }
41 41
 
42 42
 
@@ -44,7 +44,7 @@ public final class PersonWarehouse implements Iterable<Person> {
44 44
      * @return list of uniquely named Person objects
45 45
      */ //TODO
46 46
     public Stream<Person> getUniquelyNamedPeople() {
47
-        return null;
47
+        return people.stream().distinct();
48 48
     }
49 49
 
50 50
 
@@ -53,7 +53,7 @@ public final class PersonWarehouse implements Iterable<Person> {
53 53
      * @return a Stream of respective
54 54
      */ //TODO
55 55
     public Stream<Person> getUniquelyNamedPeopleStartingWith(Character character) {
56
-        return null;
56
+        return people.stream().distinct().filter(person -> person.getName().charAt(0) == character);
57 57
     }
58 58
 
59 59
     /**
@@ -61,14 +61,14 @@ public final class PersonWarehouse implements Iterable<Person> {
61 61
      * @return a Stream of respective
62 62
      */ //TODO
63 63
     public Stream<Person> getFirstNUniquelyNamedPeople(int n) {
64
-        return null;
64
+        return people.stream().distinct().limit(n);
65 65
     }
66 66
 
67 67
     /**
68 68
      * @return a mapping of Person Id to the respective Person name
69 69
      */ // TODO
70 70
     public Map<Long, String> getIdToNameMap() {
71
-        return null;
71
+        return people.stream().collect(Collectors.toMap(Person::getPersonalId, Person::getName));
72 72
     }
73 73
 
74 74
 

+ 2
- 2
src/main/java/com/zipcodewilmington/streams/conversions/ArrayConverter.java Dosyayı Görüntüle

@@ -25,12 +25,12 @@ public final class ArrayConverter extends PersonConversionAgent<Person[]> {
25 25
 
26 26
     //TODO
27 27
     public List<Person> toList() {
28
-        return null;
28
+        return Stream.of(super.objectSequence).collect(Collectors.toList());
29 29
     }
30 30
 
31 31
     //TODO
32 32
     public Stream<Person> toStream() {
33
-        return null;
33
+        return Stream.of(super.objectSequence);
34 34
     }
35 35
 
36 36
     @Override

+ 2
- 2
src/main/java/com/zipcodewilmington/streams/conversions/ListConverter.java Dosyayı Görüntüle

@@ -29,11 +29,11 @@ public final class ListConverter extends PersonConversionAgent<List<Person>> {
29 29
 
30 30
     //TODO
31 31
     public Stream<Person> toStream() {
32
-        return null;
32
+        return super.objectSequence.stream();
33 33
     }
34 34
 
35 35
     //TODO
36 36
     public Person[] toArray() {
37
-        return null;
37
+        return super.objectSequence.stream().toArray(Person[]::new);
38 38
     }
39 39
 }

+ 3
- 3
src/main/java/com/zipcodewilmington/streams/conversions/StreamConverter.java Dosyayı Görüntüle

@@ -25,16 +25,16 @@ public final class StreamConverter extends PersonConversionAgent<Stream<Person>>
25 25
 
26 26
     // TODO
27 27
     public List<Person> toList() {
28
-        return null;
28
+        return personList;
29 29
     }
30 30
 
31 31
     // TODO
32 32
     public Stream<Person> toStream() {
33
-        return null;
33
+        return personList.stream();
34 34
     }
35 35
 
36 36
     // TODO
37 37
     public Person[] toArray() {
38
-        return null;
38
+        return personList.stream().toArray(Person[]::new);
39 39
     }
40 40
 }