diff --git a/looptest.iml b/looptest.iml
index 7f2d304..792af39 100644
--- a/looptest.iml
+++ b/looptest.iml
@@ -10,10 +10,10 @@
-
+
-
-
-
+
+
+
\ No newline at end of file
diff --git a/src/main/java/com/zipcodewilmington/streams/StreamFilter.java b/src/main/java/com/zipcodewilmington/streams/StreamFilter.java
index d8b89ae..056392f 100644
--- a/src/main/java/com/zipcodewilmington/streams/StreamFilter.java
+++ b/src/main/java/com/zipcodewilmington/streams/StreamFilter.java
@@ -20,23 +20,28 @@ public class StreamFilter {
* No arg constructor
*/ //TODO - construct person stream of 100 person objects; startingCharacter is a random capital letter
public StreamFilter() {
- this(Stream.empty(), null);
+ this.personStream = new PersonFactory().createPersonStream(100);
+ this.startingCharacter = RandomUtils.createCharacter('A','Z').toString();
}
/**
* @param people - Array of person objects
* @param startingCharacter - character to filter by
- */ //TODO
+ */
public StreamFilter(Person[] people, Character startingCharacter) {
- this(Stream.empty(), null);
+
+ this.personStream = Stream.of(people);
+ this.startingCharacter = startingCharacter.toString();
}
/**
* @param people - List of person objects
* @param startingCharacter - character to filter by
- */ //TODO
+ */
public StreamFilter(List people, Character startingCharacter) {
- this(Stream.empty(), null);
+
+ this.personStream = people.stream();
+ this.startingCharacter = startingCharacter.toString();
}
@@ -53,36 +58,46 @@ public StreamFilter(Stream people, Character startingCharacter) {
/**
* Using multi-line lambda syntax
* @return a list of person object whose name starts with `this.startingCharacter`
- */ //TODO
+ */
public List toListMultiLine() {
- return null;
+
+ return this.personStream
+ .filter(person -> person.getName()
+ .startsWith(this.startingCharacter))
+ .collect(Collectors.toList());
}
/**
* Using one-line lambda syntax
* @return a list of person objects whose name starts with `this.startingCharacter`
- */ //TODO
+ */
public List toListOneLine() {
- return null;
+
+ return toListMultiLine();
}
/**
* Using one-line lambda syntax
* @return an array of person object whose name starts with `this.startingCharacter`
- */ //TODO
+ */
public Person[] toArrayOneLine() {
- return null;
+
+ return toArrayMultiLine();
}
/**
* Using multi-line lambda syntax
* @return an array of person object whose name starts with `this.startingCharacter`
- */ //TODO
+ */
public Person[] toArrayMultiLine() {
- return null;
+
+ return this.personStream
+ .filter(person -> person.getName()
+ .startsWith(this.startingCharacter))
+ .toArray(Person[]::new);
}
}
diff --git a/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonFactory.java b/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonFactory.java
index a10ec7b..9b925a5 100644
--- a/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonFactory.java
+++ b/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonFactory.java
@@ -3,6 +3,8 @@
import com.zipcodewilmington.streams.tools.RandomUtils;
import com.zipcodewilmington.streams.tools.StringUtils;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@@ -39,7 +41,9 @@ public Person createRandomPerson() {
* @return - ArrayList of Person objects
*/ // TODO
public List createPersonList(int listSize) {
- return null;
+ List result = new ArrayList<>();
+ result.addAll(Arrays.asList(createPersonArray(listSize)));
+ return result;
}
@@ -48,7 +52,9 @@ public List createPersonList(int listSize) {
* @return - Array of Person objects
*/ // TODO
public Person[] createPersonArray(int arrayLength) {
- return null;
+ Stream temp = createPersonStream(arrayLength);
+ Person [] result = temp.toArray(Person[]::new);
+ return result;
}
@@ -59,6 +65,7 @@ public Person[] createPersonArray(int arrayLength) {
* @return - Stream representation of collection of Person objects
*/ // TODO
public Stream createPersonStream(int streamCount) {
- return null;
+ Stream personStream = Stream.generate(this :: createRandomPerson).limit(streamCount);
+ return personStream;
}
}
diff --git a/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonWarehouse.java b/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonWarehouse.java
index a4960b1..1a04c97 100644
--- a/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonWarehouse.java
+++ b/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonWarehouse.java
@@ -4,10 +4,12 @@
import com.zipcodewilmington.streams.tools.logging.LoggerHandler;
import com.zipcodewilmington.streams.tools.logging.LoggerWarehouse;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
+import javax.imageio.plugins.jpeg.JPEGImageReadParam;
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -34,26 +36,44 @@ public void addPerson(Person person) {
/**
* @return list of names of Person objects
- */ // TODO
+ */
public List getNames() {
- return null;
+ List names = people.stream()
+ .map(person -> person.getName()) // apply the function to each element in a stream
+ .collect(Collectors.toList());
+
+ return names;
}
/**
* @return list of uniquely named Person objects
- */ //TODO
+ */
public Stream getUniquelyNamedPeople() {
- return null;
+
+ List uniquelyNamedPeople = people.stream()
+ .filter(distinctByName(Person::getName))
+ .collect(Collectors.toList());
+
+ return uniquelyNamedPeople.stream();
}
+ public static Predicate distinctByName(Function super Person, ?> keyExtractor) {
+ Set