katherine 7 лет назад
Родитель
Сommit
8abbd418c7

+ 13
- 0
pom.xml Просмотреть файл

@@ -4,6 +4,19 @@
4 4
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 5
     <modelVersion>4.0.0</modelVersion>
6 6
 
7
+    <build>
8
+        <plugins>
9
+            <plugin>
10
+                <groupId>org.apache.maven.plugins</groupId>
11
+                <artifactId>maven-compiler-plugin</artifactId>
12
+                <configuration>
13
+                    <source>1.8</source>
14
+                    <target>1.8</target>
15
+                </configuration>
16
+            </plugin>
17
+        </plugins>
18
+    </build>
19
+
7 20
     <groupId>mygroupid</groupId>
8 21
     <artifactId>looptest</artifactId>
9 22
     <version>1.0-SNAPSHOT</version>

Двоичные данные
src/.DS_Store Просмотреть файл


Двоичные данные
src/main/.DS_Store Просмотреть файл


+ 16
- 7
src/main/java/com/zipcodewilmington/streams/StreamFilter.java Просмотреть файл

@@ -2,10 +2,13 @@ package com.zipcodewilmington.streams;
2 2
 
3 3
 import com.zipcodewilmington.streams.anthropoid.Person;
4 4
 import com.zipcodewilmington.streams.anthropoid.PersonFactory;
5
+import com.zipcodewilmington.streams.anthropoid.PersonWarehouse;
5 6
 import com.zipcodewilmington.streams.tools.RandomUtils;
6 7
 import com.zipcodewilmington.streams.tools.StringUtils;
7 8
 
9
+import java.util.Arrays;
8 10
 import java.util.List;
11
+import java.util.Random;
9 12
 import java.util.stream.Collectors;
10 13
 import java.util.stream.Stream;
11 14
 
@@ -16,11 +19,12 @@ public class StreamFilter {
16 19
     private final Stream<Person> personStream;
17 20
     public final String startingCharacter;
18 21
 
22
+
19 23
     /**
20 24
      * No arg constructor
21 25
      */ //TODO - construct person stream of 100 person objects; startingCharacter is a random capital letter
22 26
     public StreamFilter() {
23
-        this(Stream.empty(), null);
27
+        this(PersonFactory.createPersonArray(100), RandomUtils.createCharacter('A', 'Z'));
24 28
     }
25 29
 
26 30
     /**
@@ -28,7 +32,7 @@ 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
+        this(Arrays.asList(people), startingCharacter);
32 36
     }
33 37
 
34 38
     /**
@@ -36,7 +40,7 @@ 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(people.stream(), startingCharacter);
40 44
     }
41 45
 
42 46
 
@@ -55,7 +59,10 @@ public class StreamFilter {
55 59
      * @return a list of person object whose name starts with `this.startingCharacter`
56 60
      */ //TODO
57 61
     public List<Person> toListMultiLine() {
58
-        return null;
62
+        return personStream.filter(p -> {
63
+            return p.getName().startsWith(startingCharacter);
64
+        })
65
+        .collect(Collectors.toList());
59 66
     }
60 67
 
61 68
 
@@ -64,7 +71,7 @@ public class StreamFilter {
64 71
      * @return a list of person objects whose name starts with `this.startingCharacter`
65 72
      */ //TODO
66 73
     public List<Person> toListOneLine() {
67
-        return null;
74
+        return personStream.filter(p -> (p.getName().startsWith(startingCharacter))).collect(Collectors.toList());
68 75
     }
69 76
 
70 77
 
@@ -73,7 +80,8 @@ public class StreamFilter {
73 80
      * @return an array of person object whose name starts with `this.startingCharacter`
74 81
      */ //TODO
75 82
     public Person[] toArrayOneLine() {
76
-        return null;
83
+        List<Person> filteredList = toListOneLine();
84
+        return filteredList.toArray(new Person[filteredList.size()]);
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
+        List<Person> filteredList = toListOneLine();
94
+        return filteredList.toArray(new Person[filteredList.size()]);
86 95
     }
87 96
 
88 97
 }

+ 3
- 3
src/main/java/com/zipcodewilmington/streams/StreamMap.java Просмотреть файл

@@ -18,7 +18,7 @@ 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
+        return Stream.of(someWord.split(""));
22 22
     }
23 23
 
24 24
     /**
@@ -26,7 +26,7 @@ public class StreamMap {
26 26
      * @return - a Stream of several Streams of single characters
27 27
      */ //TODO
28 28
     public static Stream<Stream<String>> wordsMap(String... someWords) {
29
-        return null;
29
+        return Stream.of(someWords).map(word -> letters(word));
30 30
     }
31 31
 
32 32
     /**
@@ -34,6 +34,6 @@ public class StreamMap {
34 34
      * @return - a Stream of several Streams of single characters
35 35
      */ //TODO
36 36
     public static Stream<String> wordsFlatMap(String... stringArray) {
37
-        return null;
37
+        return Stream.of(stringArray).flatMap(word -> letters(word));
38 38
     }
39 39
 }

+ 6
- 6
src/main/java/com/zipcodewilmington/streams/anthropoid/PersonFactory.java Просмотреть файл

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

+ 27
- 17
src/main/java/com/zipcodewilmington/streams/anthropoid/PersonWarehouse.java Просмотреть файл

@@ -4,9 +4,8 @@ import com.zipcodewilmington.streams.tools.ReflectionUtils;
4 4
 import com.zipcodewilmington.streams.tools.logging.LoggerHandler;
5 5
 import com.zipcodewilmington.streams.tools.logging.LoggerWarehouse;
6 6
 
7
-import java.util.ArrayList;
8
-import java.util.List;
9
-import java.util.Map;
7
+import java.util.*;
8
+import java.util.stream.Collector;
10 9
 import java.util.stream.Collectors;
11 10
 import java.util.stream.Stream;
12 11
 
@@ -33,56 +32,67 @@ public final class PersonWarehouse {
33 32
 
34 33
     /**
35 34
      * @return list of uniquely named Person objects
36
-     */ //TODO
35
+     */
37 36
     public static Stream<Person> getUniquelyNamedPeople() {
38
-        return null;
37
+        /*
38
+        HashMap<String, Person> uniqueMap = new HashMap<>();
39
+        people.stream().forEach(p -> uniqueMap.put(p.getName(), p));
40
+        return uniqueMap.entrySet().stream().map(e -> uniqueMap.get(e.getKey())).collect(Collectors.toList()).stream();
41
+        */
42
+        /*
43
+        List<Person> allNames = people.stream().map(p -> p.getName()).collect(Collectors.toList());
44
+        allNames = allNames.stream().distinct().collect(Collectors.toList());
45
+        */
46
+
47
+        return people.stream().collect(Collectors.toMap(Person::getName, p -> p, (p1, p2) -> p1)).values().stream();
39 48
     }
40 49
 
41 50
 
42 51
     /**
43 52
      * @param character starting character of Person objects' name
44 53
      * @return a Stream of respective
45
-     */ //TODO
54
+     */
46 55
     public static Stream<Person> getUniquelyNamedPeopleStartingWith(Character character) {
47
-        return null;
56
+        String startsWithChar = Character.toString(character);
57
+        return people.stream().distinct().filter(p -> (p.getName().startsWith(startsWithChar)));
48 58
     }
49 59
 
50 60
     /**
51 61
      * @param n first `n` Person objects
52 62
      * @return a Stream of respective
53
-     */ //TODO
63
+     */
54 64
     public static Stream<Person> getFirstNUniquelyNamedPeople(int n) {
55
-        return null;
65
+        return getUniquelyNamedPeople().limit(n);
56 66
     }
57 67
 
58 68
     /**
59 69
      * @return a mapping of Person Id to the respective Person name
60
-     */ // TODO
70
+     */
61 71
     public static Map<Long, String> getIdToNameMap() {
62
-        return null;
72
+        return people.stream().collect(Collectors.toMap(p -> p.getPersonalId(), p -> p.getName()));
63 73
     }
64 74
 
65 75
 
66 76
     /**
67 77
      * @return Stream of Stream of Aliases
68
-     */ // TODO
78
+     */
69 79
     public static Stream<Stream<String>> getNestedAliases() {
70
-        return null;
80
+        return people.stream().map(p -> Stream.of(p.getAliases()));
71 81
     }
72 82
 
73 83
 
74 84
     /**
75 85
      * @return Stream of all Aliases
76
-     */ // TODO
86
+     */
77 87
     public static Stream<String> getAllAliases() {
78
-        return null;
88
+        return people.stream().flatMap(p -> Stream.of(p.getAliases()));
79 89
     }
80 90
 
81 91
     /**
82 92
      * @return list of names of Person objects
83
-     */ // TODO
93
+     */
84 94
     public static List<String> getNames() {
85
-        return null;
95
+        return people.stream().map(p -> p.getName()).collect(Collectors.toList());
86 96
     }
87 97
 
88 98
     /**

+ 2
- 1
src/test/java/com/zipcodewilmington/streams/anthropoid/TestPersonWarehouse.java Просмотреть файл

@@ -14,10 +14,11 @@ import java.util.stream.Collectors;
14 14
  */
15 15
 public class TestPersonWarehouse {
16 16
 
17
+
17 18
     @Before
18 19
     public void setup() {
19 20
         PersonWarehouse.getPeople().clear();
20
-        PersonFactory.createPersonStream(999);
21
+        PersonFactory.createPersonStream(999).collect(Collectors.toList());
21 22
     }
22 23
 
23 24
     @Test