De'Jon Johnson 6 anni fa
parent
commit
a04cc032d2

+ 2
- 0
.idea/compiler.xml Vedi File

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

+ 0
- 13
.idea/libraries/Maven__junit_junit_4_12.xml Vedi File

1
-<component name="libraryTable">
2
-  <library name="Maven: junit:junit:4.12">
3
-    <CLASSES>
4
-      <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
5
-    </CLASSES>
6
-    <JAVADOC>
7
-      <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12-javadoc.jar!/" />
8
-    </JAVADOC>
9
-    <SOURCES>
10
-      <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12-sources.jar!/" />
11
-    </SOURCES>
12
-  </library>
13
-</component>

+ 13
- 0
.idea/libraries/Maven__junit_junit_4_13_beta_1.xml Vedi File

1
+<component name="libraryTable">
2
+  <library name="Maven: junit:junit:4.13-beta-1">
3
+    <CLASSES>
4
+      <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13-beta-1/junit-4.13-beta-1.jar!/" />
5
+    </CLASSES>
6
+    <JAVADOC>
7
+      <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13-beta-1/junit-4.13-beta-1-javadoc.jar!/" />
8
+    </JAVADOC>
9
+    <SOURCES>
10
+      <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13-beta-1/junit-4.13-beta-1-sources.jar!/" />
11
+    </SOURCES>
12
+  </library>
13
+</component>

+ 457
- 853
.idea/workspace.xml
File diff suppressed because it is too large
Vedi File


+ 12
- 0
pom.xml Vedi File

7
     <groupId>mygroupid</groupId>
7
     <groupId>mygroupid</groupId>
8
     <artifactId>looptest</artifactId>
8
     <artifactId>looptest</artifactId>
9
     <version>1.0-SNAPSHOT</version>
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
     <dependencies>
22
     <dependencies>
11
         <dependency>
23
         <dependency>
12
             <groupId>junit</groupId>
24
             <groupId>junit</groupId>

+ 23
- 8
src/main/java/com/zipcodewilmington/streams/StreamFilter.java Vedi File

5
 import com.zipcodewilmington.streams.tools.RandomUtils;
5
 import com.zipcodewilmington.streams.tools.RandomUtils;
6
 import com.zipcodewilmington.streams.tools.StringUtils;
6
 import com.zipcodewilmington.streams.tools.StringUtils;
7
 
7
 
8
+import java.util.Arrays;
8
 import java.util.List;
9
 import java.util.List;
10
+import java.util.function.Predicate;
9
 import java.util.stream.Collectors;
11
 import java.util.stream.Collectors;
10
 import java.util.stream.Stream;
12
 import java.util.stream.Stream;
11
 
13
 
14
  */
16
  */
15
 public class StreamFilter {
17
 public class StreamFilter {
16
     private final Stream<Person> personStream;
18
     private final Stream<Person> personStream;
17
-    public final String startingCharacter;
19
+    private final String startingCharacter;
20
+
18
 
21
 
19
     /**
22
     /**
20
      * No arg constructor
23
      * No arg constructor
21
      */ //TODO - construct person stream of 100 person objects; startingCharacter is a random capital letter
24
      */ //TODO - construct person stream of 100 person objects; startingCharacter is a random capital letter
22
     public StreamFilter() {
25
     public StreamFilter() {
23
-        this(Stream.empty(), null);
26
+
27
+        this(PersonFactory.createPersonStream(10), 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
+
36
+        this(Arrays.stream(people), startingCharacter);
32
     }
37
     }
33
 
38
 
34
     /**
39
     /**
36
      * @param startingCharacter - character to filter by
41
      * @param startingCharacter - character to filter by
37
      */ //TODO
42
      */ //TODO
38
     public StreamFilter(List<Person> people, Character startingCharacter) {
43
     public StreamFilter(List<Person> people, Character startingCharacter) {
39
-        this(Stream.empty(), null);
44
+        this(people.stream(), startingCharacter);
40
     }
45
     }
41
 
46
 
42
 
47
 
44
      * @param people - Stream of person objects
49
      * @param people - Stream of person objects
45
      * @param startingCharacter - character to filter by
50
      * @param startingCharacter - character to filter by
46
      */ // I took care of the easy constructor (͡° ͜ʖ ͡°)
51
      */ // I took care of the easy constructor (͡° ͜ʖ ͡°)
52
+
47
     public StreamFilter(Stream<Person> people, Character startingCharacter) {
53
     public StreamFilter(Stream<Person> people, Character startingCharacter) {
48
         this.personStream = people;
54
         this.personStream = people;
49
         this.startingCharacter = startingCharacter.toString();
55
         this.startingCharacter = startingCharacter.toString();
55
      * @return a list of person object whose name starts with `this.startingCharacter`
61
      * @return a list of person object whose name starts with `this.startingCharacter`
56
      */ //TODO
62
      */ //TODO
57
     public List<Person> toListMultiLine() {
63
     public List<Person> toListMultiLine() {
58
-        return null;
64
+
65
+
66
+        Predicate<Person> filter = person -> StringUtils.isPalindromeIgnoreCase(person.getName());
67
+         return personStream
68
+             .filter(filter)
69
+             .collect(Collectors.toList());
59
     }
70
     }
60
 
71
 
61
 
72
 
64
      * @return a list of person objects whose name starts with `this.startingCharacter`
75
      * @return a list of person objects whose name starts with `this.startingCharacter`
65
      */ //TODO
76
      */ //TODO
66
     public List<Person> toListOneLine() {
77
     public List<Person> toListOneLine() {
67
-        return null;
78
+        return personStream.filter(person -> StringUtils.isPalindromeIgnoreCase(person.getName())).collect(Collectors.toList());
68
     }
79
     }
69
 
80
 
70
 
81
 
73
      * @return an array of person object whose name starts with `this.startingCharacter`
84
      * @return an array of person object whose name starts with `this.startingCharacter`
74
      */ //TODO
85
      */ //TODO
75
     public Person[] toArrayOneLine() {
86
     public Person[] toArrayOneLine() {
76
-        return null;
87
+        return personStream.filter(person -> StringUtils.isPalindromeIgnoreCase(person.getName())).toArray(Person[]::new);
77
     }
88
     }
78
 
89
 
79
 
90
 
82
      * @return an array of person object whose name starts with `this.startingCharacter`
93
      * @return an array of person object whose name starts with `this.startingCharacter`
83
      */ //TODO
94
      */ //TODO
84
     public Person[] toArrayMultiLine() {
95
     public Person[] toArrayMultiLine() {
85
-        return null;
96
+        return personStream.filter(person -> {StringBuilder reverseName = new StringBuilder();
97
+          for (int i = person.getName().length() - 1; i > 0; i--) {
98
+               reverseName.append(person.getName().charAt(i));
99
+              } return reverseName.toString().equalsIgnoreCase(person.getName());
100
+         }).toArray(Person[]::new);
86
     }
101
     }
87
 
102
 
88
 }
103
 }

+ 4
- 3
src/main/java/com/zipcodewilmington/streams/StreamMap.java Vedi 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
+        String[] characters = someWord.split("");
22
+        return Arrays.stream(characters);
22
     }
23
     }
23
 
24
 
24
     /**
25
     /**
26
      * @return - a Stream of several Streams of single characters
27
      * @return - a Stream of several Streams of single characters
27
      */ //TODO
28
      */ //TODO
28
     public static Stream<Stream<String>> wordsMap(String... someWords) {
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
      * @return - a Stream of several Streams of single characters
35
      * @return - a Stream of several Streams of single characters
35
      */ //TODO
36
      */ //TODO
36
     public static Stream<String> wordsFlatMap(String... stringArray) {
37
     public static Stream<String> wordsFlatMap(String... stringArray) {
37
-        return null;
38
+        return Stream.of(stringArray).flatMap(word -> letters(word));
38
     }
39
     }
39
 }
40
 }

+ 5
- 3
src/main/java/com/zipcodewilmington/streams/anthropoid/PersonFactory.java Vedi File

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

+ 5
- 4
src/main/java/com/zipcodewilmington/streams/anthropoid/PersonWarehouse.java Vedi File

35
      * @return list of uniquely named Person objects
35
      * @return list of uniquely named Person objects
36
      */ //TODO
36
      */ //TODO
37
     public static Stream<Person> getUniquelyNamedPeople() {
37
     public static Stream<Person> getUniquelyNamedPeople() {
38
-        return null;
38
+        return people.stream().distinct();
39
     }
39
     }
40
 
40
 
41
 
41
 
44
      * @return a Stream of respective
44
      * @return a Stream of respective
45
      */ //TODO
45
      */ //TODO
46
     public static Stream<Person> getUniquelyNamedPeopleStartingWith(Character character) {
46
     public static Stream<Person> getUniquelyNamedPeopleStartingWith(Character character) {
47
-        return null;
47
+        return people.stream().distinct().filter(person -> person.getName().charAt(0) == character);
48
     }
48
     }
49
 
49
 
50
     /**
50
     /**
52
      * @return a Stream of respective
52
      * @return a Stream of respective
53
      */ //TODO
53
      */ //TODO
54
     public static Stream<Person> getFirstNUniquelyNamedPeople(int n) {
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
      * @return a mapping of Person Id to the respective Person name
60
      * @return a mapping of Person Id to the respective Person name
60
      */ // TODO
61
      */ // TODO
61
     public static Map<Long, String> getIdToNameMap() {
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
 

+ 4
- 2
src/main/java/com/zipcodewilmington/streams/conversions/ArrayConverter.java Vedi File

5
 
5
 
6
 import java.util.Arrays;
6
 import java.util.Arrays;
7
 import java.util.List;
7
 import java.util.List;
8
+import java.util.stream.Collectors;
8
 import java.util.stream.Stream;
9
 import java.util.stream.Stream;
9
 
10
 
10
 /**
11
 /**
23
 
24
 
24
     //TODO
25
     //TODO
25
     public List<Person> toList() {
26
     public List<Person> toList() {
26
-        return null;
27
+
28
+        return Stream.of(super.objectSequence).collect(Collectors.toList());
27
     }
29
     }
28
 
30
 
29
     //TODO
31
     //TODO
30
     public Stream<Person> toStream() {
32
     public Stream<Person> toStream() {
31
-        return null;
33
+        return Stream.of(super.objectSequence);
32
     }
34
     }
33
 
35
 
34
     @Override
36
     @Override

+ 4
- 2
src/main/java/com/zipcodewilmington/streams/conversions/ListConverter.java Vedi File

22
 
22
 
23
     @Override
23
     @Override
24
     public List<Person> toList() {
24
     public List<Person> toList() {
25
+
25
         return super.objectSequence;
26
         return super.objectSequence;
26
     }
27
     }
27
 
28
 
28
     //TODO
29
     //TODO
29
     public Stream<Person> toStream() {
30
     public Stream<Person> toStream() {
30
-        return null;
31
+
32
+        return super.objectSequence.stream();
31
     }
33
     }
32
 
34
 
33
     //TODO
35
     //TODO
34
     public Person[] toArray() {
36
     public Person[] toArray() {
35
-        return null;
37
+        return super.objectSequence.stream().toArray(Person[]::new);
36
     }
38
     }
37
 }
39
 }

+ 5
- 3
src/main/java/com/zipcodewilmington/streams/conversions/StreamConverter.java Vedi File

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

+ 1
- 1
streamdemo.iml Vedi File

11
     </content>
11
     </content>
12
     <orderEntry type="inheritedJdk" />
12
     <orderEntry type="inheritedJdk" />
13
     <orderEntry type="sourceFolder" forTests="false" />
13
     <orderEntry type="sourceFolder" forTests="false" />
14
-    <orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
14
+    <orderEntry type="library" name="Maven: junit:junit:4.13-beta-1" level="project" />
15
     <orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
15
     <orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
16
   </component>
16
   </component>
17
 </module>
17
 </module>