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..cfe1431 100644
--- a/src/main/java/com/zipcodewilmington/streams/StreamFilter.java
+++ b/src/main/java/com/zipcodewilmington/streams/StreamFilter.java
@@ -5,7 +5,10 @@
import com.zipcodewilmington.streams.tools.RandomUtils;
import com.zipcodewilmington.streams.tools.StringUtils;
+import java.util.Arrays;
import java.util.List;
+import java.util.Random;
+import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -20,7 +23,7 @@ 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(new PersonFactory().createPersonStream(100), RandomUtils.createCharacter('A','Z'));
}
/**
@@ -28,7 +31,8 @@ public StreamFilter() {
* @param startingCharacter - character to filter by
*/ //TODO
public StreamFilter(Person[] people, Character startingCharacter) {
- this(Stream.empty(), null);
+ this.personStream = Arrays.stream(people);
+ this.startingCharacter = startingCharacter.toString();
}
/**
@@ -36,7 +40,8 @@ public StreamFilter(Person[] people, Character startingCharacter) {
* @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();
}
@@ -55,7 +60,11 @@ public StreamFilter(Stream people, Character startingCharacter) {
* @return a list of person object whose name starts with `this.startingCharacter`
*/ //TODO
public List toListMultiLine() {
- return null;
+ return personStream.filter(person ->
+ String.valueOf(person.getName()
+ .charAt(0))
+ .equals(this.startingCharacter))
+ .collect(Collectors.toList());
}
@@ -64,7 +73,7 @@ public List toListMultiLine() {
* @return a list of person objects whose name starts with `this.startingCharacter`
*/ //TODO
public List toListOneLine() {
- return null;
+ return personStream.filter(person -> String.valueOf(person.getName().charAt(0)).equals(this.startingCharacter)).collect(Collectors.toList());
}
@@ -73,7 +82,7 @@ public List toListOneLine() {
* @return an array of person object whose name starts with `this.startingCharacter`
*/ //TODO
public Person[] toArrayOneLine() {
- return null;
+ return personStream.filter(person -> String.valueOf(person.getName().charAt(0)).equals(this.startingCharacter)).toArray(Person[]::new);
}
@@ -82,7 +91,11 @@ public Person[] toArrayOneLine() {
* @return an array of person object whose name starts with `this.startingCharacter`
*/ //TODO
public Person[] toArrayMultiLine() {
- return null;
+ return personStream.filter(person ->
+ String.valueOf(person.getName()
+ .charAt(0))
+ .equals(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..0ff9aa6 100644
--- a/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonFactory.java
+++ b/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonFactory.java
@@ -3,6 +3,7 @@
import com.zipcodewilmington.streams.tools.RandomUtils;
import com.zipcodewilmington.streams.tools.StringUtils;
+import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@@ -14,6 +15,10 @@
* @ATTENTION_TO_STUDENTS You are FORBIDDEN from using loops of any sort within the definition of this class.
*/
public final class PersonFactory {
+ private List personList;
+ private Person[] personArray;
+ private Stream personStream;
+
public PersonFactory() {
/** this class is not to be instantiated */
}
@@ -39,7 +44,11 @@ public Person createRandomPerson() {
* @return - ArrayList of Person objects
*/ // TODO
public List createPersonList(int listSize) {
- return null;
+ List personList = new ArrayList<>();
+ for(int i = 0; i < listSize; i++){
+ personList.add(createRandomPerson());
+ }
+ return personList;
}
@@ -48,7 +57,11 @@ public List createPersonList(int listSize) {
* @return - Array of Person objects
*/ // TODO
public Person[] createPersonArray(int arrayLength) {
- return null;
+ Person[] personArray = new Person[arrayLength];
+ for(int i = 0; i < arrayLength; i++){
+ personArray[i] = createRandomPerson();
+ }
+ return personArray;
}
@@ -59,6 +72,7 @@ public Person[] createPersonArray(int arrayLength) {
* @return - Stream representation of collection of Person objects
*/ // TODO
public Stream createPersonStream(int streamCount) {
- return null;
+ List personStream = createPersonList(streamCount);
+ return personStream.stream();
}
}
diff --git a/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonWarehouse.java b/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonWarehouse.java
index a4960b1..22f6792 100644
--- a/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonWarehouse.java
+++ b/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonWarehouse.java
@@ -4,10 +4,9 @@
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 java.util.*;
+import java.util.function.Function;
+import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -36,7 +35,7 @@ public void addPerson(Person person) {
* @return list of names of Person objects
*/ // TODO
public List getNames() {
- return null;
+ return people.stream().map(Person::getName).collect(Collectors.toList());
}
@@ -44,7 +43,7 @@ public List getNames() {
* @return list of uniquely named Person objects
*/ //TODO
public Stream getUniquelyNamedPeople() {
- return null;
+ return people.stream().filter(distinctByKey(Person::getName));
}
@@ -53,7 +52,7 @@ public Stream getUniquelyNamedPeople() {
* @return a Stream of respective
*/ //TODO
public Stream getUniquelyNamedPeopleStartingWith(Character character) {
- return null;
+ return people.stream().filter(person -> (person.getName().indexOf(character) == 0));
}
/**
@@ -61,14 +60,14 @@ public Stream getUniquelyNamedPeopleStartingWith(Character character) {
* @return a Stream of respective
*/ //TODO
public Stream getFirstNUniquelyNamedPeople(int n) {
- return null;
+ return getUniquelyNamedPeople().limit(n);
}
/**
* @return a mapping of Person Id to the respective Person name
*/ // TODO
public Map getIdToNameMap() {
- return null;
+ return people.stream().collect(Collectors.toMap(Person::getPersonalId,Person::getName));
}
@@ -76,6 +75,7 @@ public Map getIdToNameMap() {
* @return Stream of Stream of Aliases
*/ // TODO
public Stream> getNestedAliases() {
+ //return people.stream().flatMap(person -> Stream.of(person.getAliases()));
return null;
}
@@ -84,7 +84,9 @@ public Stream> getNestedAliases() {
* @return Stream of all Aliases
*/ // TODO
public Stream getAllAliases() {
- return null;
+ return people.stream().flatMap(person -> Arrays.stream(person.getAliases()));
+ // return people.stream().flatMap(person -> Stream.of(person.getAliases()));
+
}
// DO NOT MODIFY
@@ -106,4 +108,10 @@ public int size() {
public Iterator iterator() {
return people.iterator();
}
+
+ public static Predicate distinctByKey(Function super T, Object> keyExtractor)
+ {
+ Map