Browse Source

it compiles

katherine 7 years ago
parent
commit
8abbd418c7

+ 13
- 0
pom.xml View File

4
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
     <modelVersion>4.0.0</modelVersion>
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
     <groupId>mygroupid</groupId>
20
     <groupId>mygroupid</groupId>
8
     <artifactId>looptest</artifactId>
21
     <artifactId>looptest</artifactId>
9
     <version>1.0-SNAPSHOT</version>
22
     <version>1.0-SNAPSHOT</version>

BIN
src/.DS_Store View File


BIN
src/main/.DS_Store View File


+ 16
- 7
src/main/java/com/zipcodewilmington/streams/StreamFilter.java View File

2
 
2
 
3
 import com.zipcodewilmington.streams.anthropoid.Person;
3
 import com.zipcodewilmington.streams.anthropoid.Person;
4
 import com.zipcodewilmington.streams.anthropoid.PersonFactory;
4
 import com.zipcodewilmington.streams.anthropoid.PersonFactory;
5
+import com.zipcodewilmington.streams.anthropoid.PersonWarehouse;
5
 import com.zipcodewilmington.streams.tools.RandomUtils;
6
 import com.zipcodewilmington.streams.tools.RandomUtils;
6
 import com.zipcodewilmington.streams.tools.StringUtils;
7
 import com.zipcodewilmington.streams.tools.StringUtils;
7
 
8
 
9
+import java.util.Arrays;
8
 import java.util.List;
10
 import java.util.List;
11
+import java.util.Random;
9
 import java.util.stream.Collectors;
12
 import java.util.stream.Collectors;
10
 import java.util.stream.Stream;
13
 import java.util.stream.Stream;
11
 
14
 
16
     private final Stream<Person> personStream;
19
     private final Stream<Person> personStream;
17
     public final String startingCharacter;
20
     public final String startingCharacter;
18
 
21
 
22
+
19
     /**
23
     /**
20
      * No arg constructor
24
      * No arg constructor
21
      */ //TODO - construct person stream of 100 person objects; startingCharacter is a random capital letter
25
      */ //TODO - construct person stream of 100 person objects; startingCharacter is a random capital letter
22
     public StreamFilter() {
26
     public StreamFilter() {
23
-        this(Stream.empty(), null);
27
+        this(PersonFactory.createPersonArray(100), RandomUtils.createCharacter('A', 'Z'));
24
     }
28
     }
25
 
29
 
26
     /**
30
     /**
28
      * @param startingCharacter - character to filter by
32
      * @param startingCharacter - character to filter by
29
      */ //TODO
33
      */ //TODO
30
     public StreamFilter(Person[] people, Character startingCharacter) {
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
      * @param startingCharacter - character to filter by
40
      * @param startingCharacter - character to filter by
37
      */ //TODO
41
      */ //TODO
38
     public StreamFilter(List<Person> people, Character startingCharacter) {
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
      * @return a list of person object whose name starts with `this.startingCharacter`
59
      * @return a list of person object whose name starts with `this.startingCharacter`
56
      */ //TODO
60
      */ //TODO
57
     public List<Person> toListMultiLine() {
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
      * @return a list of person objects whose name starts with `this.startingCharacter`
71
      * @return a list of person objects whose name starts with `this.startingCharacter`
65
      */ //TODO
72
      */ //TODO
66
     public List<Person> toListOneLine() {
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
      * @return an array of person object whose name starts with `this.startingCharacter`
80
      * @return an array of person object whose name starts with `this.startingCharacter`
74
      */ //TODO
81
      */ //TODO
75
     public Person[] toArrayOneLine() {
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
      * @return an array of person object whose name starts with `this.startingCharacter`
90
      * @return an array of person object whose name starts with `this.startingCharacter`
83
      */ //TODO
91
      */ //TODO
84
     public Person[] toArrayMultiLine() {
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 View File

18
      * @return - a Stream of single characters
18
      * @return - a Stream of single characters
19
      */ //TODO
19
      */ //TODO
20
     public static Stream<String> letters(String someWord) {
20
     public static Stream<String> letters(String someWord) {
21
-        return null;
21
+        return Stream.of(someWord.split(""));
22
     }
22
     }
23
 
23
 
24
     /**
24
     /**
26
      * @return - a Stream of several Streams of single characters
26
      * @return - a Stream of several Streams of single characters
27
      */ //TODO
27
      */ //TODO
28
     public static Stream<Stream<String>> wordsMap(String... someWords) {
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
      * @return - a Stream of several Streams of single characters
34
      * @return - a Stream of several Streams of single characters
35
      */ //TODO
35
      */ //TODO
36
     public static Stream<String> wordsFlatMap(String... stringArray) {
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 View File

37
      * Section 8.8
37
      * Section 8.8
38
      * @param listSize - number of Person objects to create
38
      * @param listSize - number of Person objects to create
39
      * @return - ArrayList of Person objects
39
      * @return - ArrayList of Person objects
40
-     */ // TODO
40
+     */
41
     public static List<Person> createPersonList(int listSize) {
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
      * @param arrayLength - number of Person objects to create
47
      * @param arrayLength - number of Person objects to create
48
      * @return - Array of Person objects
48
      * @return - Array of Person objects
49
-     */ // TODO
49
+     */
50
     public static Person[] createPersonArray(int arrayLength) {
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
      * Section 8.2
56
      * Section 8.2
57
      * @param streamCount - number of Person objects to create
57
      * @param streamCount - number of Person objects to create
58
      * @return - Stream representation of collection of Person objects
58
      * @return - Stream representation of collection of Person objects
59
-     */ // TODO
59
+     */
60
     public static Stream<Person> createPersonStream(int streamCount) {
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 View File

4
 import com.zipcodewilmington.streams.tools.logging.LoggerHandler;
4
 import com.zipcodewilmington.streams.tools.logging.LoggerHandler;
5
 import com.zipcodewilmington.streams.tools.logging.LoggerWarehouse;
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
 import java.util.stream.Collectors;
9
 import java.util.stream.Collectors;
11
 import java.util.stream.Stream;
10
 import java.util.stream.Stream;
12
 
11
 
33
 
32
 
34
     /**
33
     /**
35
      * @return list of uniquely named Person objects
34
      * @return list of uniquely named Person objects
36
-     */ //TODO
35
+     */
37
     public static Stream<Person> getUniquelyNamedPeople() {
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
      * @param character starting character of Person objects' name
52
      * @param character starting character of Person objects' name
44
      * @return a Stream of respective
53
      * @return a Stream of respective
45
-     */ //TODO
54
+     */
46
     public static Stream<Person> getUniquelyNamedPeopleStartingWith(Character character) {
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
      * @param n first `n` Person objects
61
      * @param n first `n` Person objects
52
      * @return a Stream of respective
62
      * @return a Stream of respective
53
-     */ //TODO
63
+     */
54
     public static Stream<Person> getFirstNUniquelyNamedPeople(int n) {
64
     public static Stream<Person> getFirstNUniquelyNamedPeople(int n) {
55
-        return null;
65
+        return getUniquelyNamedPeople().limit(n);
56
     }
66
     }
57
 
67
 
58
     /**
68
     /**
59
      * @return a mapping of Person Id to the respective Person name
69
      * @return a mapping of Person Id to the respective Person name
60
-     */ // TODO
70
+     */
61
     public static Map<Long, String> getIdToNameMap() {
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
      * @return Stream of Stream of Aliases
77
      * @return Stream of Stream of Aliases
68
-     */ // TODO
78
+     */
69
     public static Stream<Stream<String>> getNestedAliases() {
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
      * @return Stream of all Aliases
85
      * @return Stream of all Aliases
76
-     */ // TODO
86
+     */
77
     public static Stream<String> getAllAliases() {
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
      * @return list of names of Person objects
92
      * @return list of names of Person objects
83
-     */ // TODO
93
+     */
84
     public static List<String> getNames() {
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 View File

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