diff --git a/looptest.iml b/looptest.iml
deleted file mode 100644
index 7f2d304..0000000
--- a/looptest.iml
+++ /dev/null
@@ -1,19 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ 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..0cb71ba 100644
--- a/src/main/java/com/zipcodewilmington/streams/StreamFilter.java
+++ b/src/main/java/com/zipcodewilmington/streams/StreamFilter.java
@@ -5,7 +5,9 @@
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.stream.Collectors;
import java.util.stream.Stream;
@@ -20,7 +22,11 @@ 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);
+
+ //ONLY CAN HAVE STATIC METHODS CALLED IN THE CONSTRUCTOR because it creates the object. We can't use the
+ //methods in the class because we don't yet have an object to call them.
+
+ this(new PersonFactory().createPersonStream(100), RandomUtils.createCharacter('A', 'Z'));
}
/**
@@ -28,7 +34,11 @@ public StreamFilter() {
* @param startingCharacter - character to filter by
*/ //TODO
public StreamFilter(Person[] people, Character startingCharacter) {
- this(Stream.empty(), null);
+
+ //if we have "this ( Stream.empty(), startingCharacter null)" with parenthesis, we're calling another constructor specifically going to call the
+ //constructor that has those two parameters,
+
+ this(Arrays.stream(people), startingCharacter);
}
/**
@@ -36,7 +46,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(people.stream(), startingCharacter);
}
@@ -55,7 +66,10 @@ 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(x -> {
+ boolean b = x.getName().matches(this.startingCharacter);
+ return b;
+ }).collect(Collectors.toList());
}
@@ -64,7 +78,9 @@ public List toListMultiLine() {
* @return a list of person objects whose name starts with `this.startingCharacter`
*/ //TODO
public List toListOneLine() {
- return null;
+ return personStream.filter(x -> x.getName()
+ .matches(this.startingCharacter))
+ .collect(Collectors.toList());
}
@@ -73,7 +89,8 @@ public List toListOneLine() {
* @return an array of person object whose name starts with `this.startingCharacter`
*/ //TODO
public Person[] toArrayOneLine() {
- return null;
+ return personStream.filter(x -> x.getName().matches(this.startingCharacter))
+ .toArray(Person[]::new);
}
@@ -82,7 +99,21 @@ public Person[] toArrayOneLine() {
* @return an array of person object whose name starts with `this.startingCharacter`
*/ //TODO
public Person[] toArrayMultiLine() {
- return null;
+ return personStream.filter(x -> x.getName().matches(this.startingCharacter))
+ .toArray(Person[]::new);
+ }
+
+ public Character createRandomLetter () {
+// Random rnd = new Random();
+// char c = (char) (rnd.nextInt(26) + 'a');
+
+// Stream.generate(Math::random)
+// .limit(limitTerms)
+
+ String chars = "abcdefghijklmnopqrstuvxyz";
+ Random rnd = new Random();
+ char c = chars.charAt(rnd.nextInt(chars.length()));
+ return c;
}
}
diff --git a/src/main/java/com/zipcodewilmington/streams/StreamMap.java b/src/main/java/com/zipcodewilmington/streams/StreamMap.java
index cf35024..94676b7 100644
--- a/src/main/java/com/zipcodewilmington/streams/StreamMap.java
+++ b/src/main/java/com/zipcodewilmington/streams/StreamMap.java
@@ -5,6 +5,7 @@
import java.util.Arrays;
import java.util.List;
+import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -18,7 +19,8 @@ public class StreamMap {
* @return - a Stream of single characters
*/ //TODO
public static Stream letters(String someWord) {
- return null;
+ String [] wordArray = someWord.split("");
+ return Arrays.stream(wordArray);
}
/**
@@ -26,7 +28,10 @@ public static Stream letters(String someWord) {
* @return - a Stream of several Streams of single characters
*/ //TODO
public static Stream> wordsMap(String... someWords) {
- return null;
+ return Stream.of(someWords).map(StreamMap::letters);
+
+ //Basically a stream of a stream is what is wanted. So we take each word, and then apply the letters method from
+ //this class on those words and return it.
}
/**
@@ -34,6 +39,9 @@ public static Stream> wordsMap(String... someWords) {
* @return - a Stream of several Streams of single characters
*/ //TODO
public static Stream wordsFlatMap(String... stringArray) {
- return null;
+
+ return wordsMap().flatMap(Function.identity());
+ //Uses the wordsMap function from this class and then returns it as flat map since the return is
+ //not nested.
}
}
\ No newline at end of file
diff --git a/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonFactory.java b/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonFactory.java
index a10ec7b..6c6209e 100644
--- a/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonFactory.java
+++ b/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonFactory.java
@@ -39,7 +39,8 @@ public Person createRandomPerson() {
* @return - ArrayList of Person objects
*/ // TODO
public List createPersonList(int listSize) {
- return null;
+ List returnedList = createPersonStream(listSize).collect(Collectors.toList());
+ return returnedList;
}
@@ -48,7 +49,7 @@ public List createPersonList(int listSize) {
* @return - Array of Person objects
*/ // TODO
public Person[] createPersonArray(int arrayLength) {
- return null;
+ return createPersonStream(arrayLength).toArray(Person[] :: new);
}
@@ -59,6 +60,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..16ee01f 100644
--- a/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonWarehouse.java
+++ b/src/main/java/com/zipcodewilmington/streams/anthropoid/PersonWarehouse.java
@@ -4,10 +4,11 @@
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.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;
@@ -36,15 +37,32 @@ 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());
+
}
/**
* @return list of uniquely named Person objects
*/ //TODO
- public Stream getUniquelyNamedPeople() {
- return null;
+ public Stream getUniquelyNamedPeople() {
+ Set mySet = new HashSet<>((people.size()));
+
+ return people.stream().filter(p -> mySet.add(p.getName()));
+
+// uniqueNames = people.stream().collect(Collectors.map(Person, getNames())
+// .entrySet()
+// .stream()
+// .filter(entry -> entry.getValue() == 1)
+// .map(Map.Entry::getKey);
+//// .collect(Collectors.toList());
+//
+ //return people.stream().map(e -> e.getName());
+//
+//stream.of(linked)
+// return uniqueNames;
+// return null;
}
@@ -53,7 +71,17 @@ public Stream getUniquelyNamedPeople() {
* @return a Stream of respective
*/ //TODO
public Stream getUniquelyNamedPeopleStartingWith(Character character) {
- return null;
+ return people.stream().filter(thePerson -> (thePerson.getName().indexOf(character) == 0));
+
+// String myChar = String.valueOf(character);
+// Predicate predicate = new Predicate (){
+//
+// @Override
+// public boolean test(String o) {
+// return (o.startsWith(myChar));
+// }
+// };
+ //people.stream().filter(predicate::apply);
}
/**
@@ -61,22 +89,33 @@ public Stream getUniquelyNamedPeopleStartingWith(Character character) {
* @return a Stream of respective
*/ //TODO
public Stream getFirstNUniquelyNamedPeople(int n) {
- return null;
+ return getUniquelyNamedPeople().limit(n);
}
+ //
+ // uniqueNames = people.stream().collect(Collectors.map(Person, getNames())
+// .entrySet()
+// .stream()
+// .filter(entry -> entry.getValue() == 1)
+// .map(Map.Entry::getKey);
+//// .collect(Collectors.toList());
+//
+
/**
* @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));
}
/**
* @return Stream of Stream of Aliases
*/ // TODO
- public Stream> getNestedAliases() {
- return null;
+ public Stream> getNestedAliases()
+ {
+ return people.stream().map(x -> Stream.of(x.getAliases()));
+
}
@@ -84,7 +123,8 @@ public Stream> getNestedAliases() {
* @return Stream of all Aliases
*/ // TODO
public Stream getAllAliases() {
- return null;
+ return getNestedAliases().flatMap(Function.identity());
+ //or inside flatMap( v -> v)
}
// DO NOT MODIFY
@@ -106,4 +146,10 @@ public int size() {
public Iterator iterator() {
return people.iterator();
}
+
+ public static Predicate distinctByKey (Function super T, Object > keyExtractor) {
+ Map