Ben Blinebury 6 年之前
父節點
當前提交
79496fb628

+ 2
- 0
.idea/compiler.xml 查看文件

@@ -7,10 +7,12 @@
7 7
         <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
8 8
         <outputRelativeToContentRoot value="true" />
9 9
         <module name="looptest" />
10
+        <module name="streamdemo" />
10 11
       </profile>
11 12
     </annotationProcessing>
12 13
     <bytecodeTargetLevel target="1.8">
13 14
       <module name="looptest" target="1.8" />
15
+      <module name="streamdemo" target="8" />
14 16
     </bytecodeTargetLevel>
15 17
   </component>
16 18
 </project>

+ 341
- 874
.idea/workspace.xml
文件差異過大導致無法顯示
查看文件


+ 12
- 0
pom.xml 查看文件

@@ -7,6 +7,18 @@
7 7
     <groupId>mygroupid</groupId>
8 8
     <artifactId>looptest</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>8</source>
17
+                    <target>8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
     <dependencies>
11 23
         <dependency>
12 24
             <groupId>junit</groupId>

+ 20
- 7
src/main/java/com/zipcodewilmington/streams/StreamFilter.java 查看文件

@@ -5,10 +5,15 @@ 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
+import static com.zipcodewilmington.streams.anthropoid.PersonFactory.createPersonStream;
15
+import static com.zipcodewilmington.streams.tools.RandomUtils.createCharacter;
16
+
12 17
 /**
13 18
  * Created by leon on 5/2/17.
14 19
  */
@@ -20,7 +25,7 @@ public class StreamFilter {
20 25
      * No arg constructor
21 26
      */ //TODO - construct person stream of 100 person objects; startingCharacter is a random capital letter
22 27
     public StreamFilter() {
23
-        this(Stream.empty(), null);
28
+        this(createPersonStream(100), createCharacter('A', 'Z'));
24 29
     }
25 30
 
26 31
     /**
@@ -28,7 +33,7 @@ public class StreamFilter {
28 33
      * @param startingCharacter - character to filter by
29 34
      */ //TODO
30 35
     public StreamFilter(Person[] people, Character startingCharacter) {
31
-        this(Stream.empty(), null);
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
 
@@ -55,7 +60,11 @@ public class StreamFilter {
55 60
      * @return a list of person object whose name starts with `this.startingCharacter`
56 61
      */ //TODO
57 62
     public List<Person> toListMultiLine() {
58
-        return null;
63
+        Predicate<Person> guy = person -> {
64
+            String name = person.getName();
65
+            return name.equalsIgnoreCase(this.startingCharacter);
66
+        };
67
+        return personStream.filter(guy).collect(Collectors.toList());
59 68
     }
60 69
 
61 70
 
@@ -64,7 +73,7 @@ public class StreamFilter {
64 73
      * @return a list of person objects whose name starts with `this.startingCharacter`
65 74
      */ //TODO
66 75
     public List<Person> toListOneLine() {
67
-        return null;
76
+        return this.personStream.filter(person -> person.getName().equalsIgnoreCase(this.startingCharacter)).collect(Collectors.toList());
68 77
     }
69 78
 
70 79
 
@@ -73,7 +82,7 @@ public class StreamFilter {
73 82
      * @return an array of person object whose name starts with `this.startingCharacter`
74 83
      */ //TODO
75 84
     public Person[] toArrayOneLine() {
76
-        return null;
85
+        return this.personStream.filter(person -> person.getName().equalsIgnoreCase(this.startingCharacter)).toArray(Person[]::new);
77 86
     }
78 87
 
79 88
 
@@ -82,7 +91,11 @@ public class StreamFilter {
82 91
      * @return an array of person object whose name starts with `this.startingCharacter`
83 92
      */ //TODO
84 93
     public Person[] toArrayMultiLine() {
85
-        return null;
94
+        Predicate<Person> guy = person -> {
95
+            String name = person.getName();
96
+            return name.equalsIgnoreCase(this.startingCharacter);
97
+        };
98
+        return personStream.filter(guy).toArray(Person[]::new);
86 99
     }
87 100
 
88 101
 }

+ 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 Arrays.stream(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 Arrays.stream(someWords).map(StreamMap::letters);
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 Arrays.stream(stringArray).flatMap(StreamMap::letters);
38 38
     }
39 39
 }

+ 6
- 3
src/main/java/com/zipcodewilmington/streams/anthropoid/PersonFactory.java 查看文件

@@ -3,6 +3,7 @@ package com.zipcodewilmington.streams.anthropoid;
3 3
 import com.zipcodewilmington.streams.tools.RandomUtils;
4 4
 import com.zipcodewilmington.streams.tools.StringUtils;
5 5
 
6
+import java.util.ArrayList;
6 7
 import java.util.Date;
7 8
 import java.util.List;
8 9
 import java.util.stream.Collectors;
@@ -39,7 +40,8 @@ public final class PersonFactory {
39 40
      * @return - ArrayList of Person objects
40 41
      */ // TODO
41 42
     public static List<Person> createPersonList(int listSize) {
42
-        return null;
43
+        Stream<Person>rosterBuilder = Stream.generate(() -> createRandomPerson()).limit(listSize);
44
+        return rosterBuilder.collect(Collectors.toList());
43 45
     }
44 46
 
45 47
 
@@ -48,7 +50,8 @@ public final class PersonFactory {
48 50
      * @return - Array of Person objects
49 51
      */ // TODO
50 52
     public static Person[] createPersonArray(int arrayLength) {
51
-        return null;
53
+        Stream<Person>rosterBuilder = Stream.generate(() -> createRandomPerson()).limit(arrayLength);
54
+        return rosterBuilder.toArray(size -> new Person[size]);
52 55
     }
53 56
 
54 57
 
@@ -58,6 +61,6 @@ public final class PersonFactory {
58 61
      * @return - Stream representation of collection of Person objects
59 62
      */ // TODO
60 63
     public static Stream<Person> createPersonStream(int streamCount) {
61
-        return null;
64
+        return Stream.generate(() -> createRandomPerson()).limit(streamCount);
62 65
     }
63 66
 }

+ 8
- 7
src/main/java/com/zipcodewilmington/streams/anthropoid/PersonWarehouse.java 查看文件

@@ -35,7 +35,7 @@ public final class PersonWarehouse {
35 35
      * @return list of uniquely named Person objects
36 36
      */ //TODO
37 37
     public static Stream<Person> getUniquelyNamedPeople() {
38
-        return null;
38
+        return people.stream().distinct();
39 39
     }
40 40
 
41 41
 
@@ -44,7 +44,7 @@ public final class PersonWarehouse {
44 44
      * @return a Stream of respective
45 45
      */ //TODO
46 46
     public static Stream<Person> getUniquelyNamedPeopleStartingWith(Character character) {
47
-        return null;
47
+        return people.stream().filter(person -> person.getName().startsWith(character.toString())).distinct();
48 48
     }
49 49
 
50 50
     /**
@@ -52,14 +52,15 @@ public final class PersonWarehouse {
52 52
      * @return a Stream of respective
53 53
      */ //TODO
54 54
     public static Stream<Person> getFirstNUniquelyNamedPeople(int n) {
55
-        return null;
55
+
56
+        return people.stream().distinct().limit(n);
56 57
     }
57 58
 
58 59
     /**
59 60
      * @return a mapping of Person Id to the respective Person name
60 61
      */ // TODO
61 62
     public static Map<Long, String> getIdToNameMap() {
62
-        return null;
63
+        return people.stream().collect(Collectors.toMap(Person::getPersonalId, Person::getName));
63 64
     }
64 65
 
65 66
 
@@ -67,7 +68,7 @@ public final class PersonWarehouse {
67 68
      * @return Stream of Stream of Aliases
68 69
      */ // TODO
69 70
     public static Stream<Stream<String>> getNestedAliases() {
70
-        return null;
71
+        return people.stream().map(person -> Stream.of(person.getAliases()));
71 72
     }
72 73
 
73 74
 
@@ -75,14 +76,14 @@ public final class PersonWarehouse {
75 76
      * @return Stream of all Aliases
76 77
      */ // TODO
77 78
     public static Stream<String> getAllAliases() {
78
-        return null;
79
+        return  people.stream().map(person -> person.getAliases().toString());
79 80
     }
80 81
 
81 82
     /**
82 83
      * @return list of names of Person objects
83 84
      */ // TODO
84 85
     public static List<String> getNames() {
85
-        return null;
86
+        return  people.stream().map(person -> person.getName()).collect(Collectors.toList());
86 87
     }
87 88
 
88 89
     /**

+ 2
- 2
src/main/java/com/zipcodewilmington/streams/conversions/ArrayConverter.java 查看文件

@@ -23,12 +23,12 @@ public final class ArrayConverter extends PersonConversionAgent<Person[]> {
23 23
 
24 24
     //TODO
25 25
     public List<Person> toList() {
26
-        return null;
26
+        return Arrays.asList(toArray());
27 27
     }
28 28
 
29 29
     //TODO
30 30
     public Stream<Person> toStream() {
31
-        return null;
31
+        return Arrays.stream(toArray());
32 32
     }
33 33
 
34 34
     @Override

+ 3
- 2
src/main/java/com/zipcodewilmington/streams/conversions/ListConverter.java 查看文件

@@ -3,6 +3,7 @@ package com.zipcodewilmington.streams.conversions;
3 3
 import com.zipcodewilmington.streams.anthropoid.Person;
4 4
 import com.zipcodewilmington.streams.anthropoid.PersonFactory;
5 5
 
6
+import java.util.Arrays;
6 7
 import java.util.List;
7 8
 import java.util.stream.Stream;
8 9
 
@@ -27,11 +28,11 @@ public final class ListConverter extends PersonConversionAgent<List<Person>> {
27 28
 
28 29
     //TODO
29 30
     public Stream<Person> toStream() {
30
-        return null;
31
+        return toList().stream();
31 32
     }
32 33
 
33 34
     //TODO
34 35
     public Person[] toArray() {
35
-        return null;
36
+        return toStream().toArray(Person[]::new);
36 37
     }
37 38
 }

+ 4
- 3
src/main/java/com/zipcodewilmington/streams/conversions/StreamConverter.java 查看文件

@@ -3,6 +3,7 @@ package com.zipcodewilmington.streams.conversions;
3 3
 import com.zipcodewilmington.streams.anthropoid.Person;
4 4
 import com.zipcodewilmington.streams.anthropoid.PersonFactory;
5 5
 
6
+import java.util.Arrays;
6 7
 import java.util.List;
7 8
 import java.util.stream.Collectors;
8 9
 import java.util.stream.Stream;
@@ -25,16 +26,16 @@ public final class StreamConverter extends PersonConversionAgent<Stream<Person>>
25 26
 
26 27
     // TODO
27 28
     public List<Person> toList() {
28
-        return null;
29
+        return toStream().collect(Collectors.toList());
29 30
     }
30 31
 
31 32
     // TODO
32 33
     public Stream<Person> toStream() {
33
-        return null;
34
+        return Arrays.stream(toArray());
34 35
     }
35 36
 
36 37
     // TODO
37 38
     public Person[] toArray() {
38
-        return null;
39
+        return personList.stream().toArray(Person[]::new);
39 40
     }
40 41
 }

+ 430786
- 6
target/PersonWarehouse.leonlog
文件差異過大導致無法顯示
查看文件


二進制
target/classes/com/zipcodewilmington/streams/StreamFilter.class 查看文件


二進制
target/classes/com/zipcodewilmington/streams/StreamMap.class 查看文件


二進制
target/classes/com/zipcodewilmington/streams/anthropoid/PersonFactory.class 查看文件


二進制
target/classes/com/zipcodewilmington/streams/anthropoid/PersonWarehouse.class 查看文件


二進制
target/classes/com/zipcodewilmington/streams/conversions/ArrayConverter.class 查看文件


二進制
target/classes/com/zipcodewilmington/streams/conversions/ListConverter.class 查看文件


二進制
target/classes/com/zipcodewilmington/streams/conversions/StreamConverter.class 查看文件


+ 1
- 1
target/global.leonlog 查看文件

@@ -1,2 +1,2 @@
1
-May 31, 2017 4:45:59 PM com.zipcodewilmington.streams.tools.logging.LoggerHandler log
1
+Jul 03, 2018 3:31:01 PM com.zipcodewilmington.streams.tools.logging.LoggerHandler log
2 2
 INFO: Instantiating logger for [ com.zipcodewilmington.streams.anthropoid.PersonWarehouse ] ... 

二進制
target/test-classes/com/zipcodewilmington/streams/TestStreamMap.class 查看文件