Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions looptest.iml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" name="Maven: junit:junit:4.13-rc-1" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.5" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.0" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.5" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.10.1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.10.1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.10.1" level="project" />
</component>
</module>
14 changes: 7 additions & 7 deletions src/main/java/com/zipcodewilmington/streams/StreamFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ 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'));
}

/**
* @param people - Array of person objects
* @param startingCharacter - character to filter by
*/ //TODO
public StreamFilter(Person[] people, Character startingCharacter) {
this(Stream.empty(), null);
this(Stream.of(people), startingCharacter);
}

/**
* @param people - List of person objects
* @param startingCharacter - character to filter by
*/ //TODO
public StreamFilter(List<Person> people, Character startingCharacter) {
this(Stream.empty(), null);
this(people.stream(), startingCharacter);
}


Expand All @@ -55,7 +55,7 @@ public StreamFilter(Stream<Person> people, Character startingCharacter) {
* @return a list of person object whose name starts with `this.startingCharacter`
*/ //TODO
public List<Person> toListMultiLine() {
return null;
return this.personStream.filter(p -> p.getName().startsWith(startingCharacter)).collect(Collectors.toList());
}


Expand All @@ -64,7 +64,7 @@ public List<Person> toListMultiLine() {
* @return a list of person objects whose name starts with `this.startingCharacter`
*/ //TODO
public List<Person> toListOneLine() {
return null;
return toListMultiLine();
}


Expand All @@ -73,7 +73,7 @@ public List<Person> toListOneLine() {
* @return an array of person object whose name starts with `this.startingCharacter`
*/ //TODO
public Person[] toArrayOneLine() {
return null;
return toArrayMultiLine();
}


Expand All @@ -82,7 +82,7 @@ public Person[] toArrayOneLine() {
* @return an array of person object whose name starts with `this.startingCharacter`
*/ //TODO
public Person[] toArrayMultiLine() {
return null;
return toListMultiLine().toArray(new Person[0]);
}

}
8 changes: 5 additions & 3 deletions src/main/java/com/zipcodewilmington/streams/StreamMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@ public class StreamMap {
* @return - a Stream of single characters
*/ //TODO
public static Stream<String> letters(String someWord) {
return null;
return Arrays.stream(someWord.split(""));
}

/**
* @param someWords - variable amount of String arguments
* @return - a Stream of several Streams of single characters
*/ //TODO
public static Stream<Stream<String>> wordsMap(String... someWords) {
return null;
return Arrays.stream(someWords).map(output -> letters(output));
// could also be: return Arrays.stream(someWords).map(StreamMap::letters);
}

/**
* @param stringArray - variable amount of String arguments
* @return - a Stream of several Streams of single characters
*/ //TODO
public static Stream<String> wordsFlatMap(String... stringArray) {
return null;
return Arrays.stream(stringArray).flatMap(output -> letters(output));
// could also be: return Arrays.stream(stringArray).flatMap(StreamMap::letters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Person createRandomPerson() {
* @return - ArrayList of Person objects
*/ // TODO
public List<Person> createPersonList(int listSize) {
return null;
return Stream.generate(this::createRandomPerson).limit(listSize).collect(Collectors.toList());
}


Expand All @@ -48,7 +48,7 @@ public List<Person> createPersonList(int listSize) {
* @return - Array of Person objects
*/ // TODO
public Person[] createPersonArray(int arrayLength) {
return null;
return Stream.generate(this::createRandomPerson).limit(arrayLength).toArray(Person[]::new);
}


Expand All @@ -59,6 +59,6 @@ public Person[] createPersonArray(int arrayLength) {
* @return - Stream representation of collection of Person objects
*/ // TODO
public Stream<Person> createPersonStream(int streamCount) {
return null;
return Stream.generate(this::createRandomPerson).limit(streamCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -36,15 +37,23 @@ public void addPerson(Person person) {
* @return list of names of Person objects
*/ // TODO
public List<String> getNames() {
return null;
return people.stream().map(Person::getName).collect(Collectors.toList());
}


/**
* @return list of uniquely named Person objects
*/ //TODO
public Stream<Person> getUniquelyNamedPeople() {
return null;
ArrayList<String> names = new ArrayList<>();
return people.stream().filter(unique -> {
if (!names.contains(unique.getName())){
names.add(unique.getName());
return true;
}
else
return false;
});
}


Expand All @@ -53,38 +62,39 @@ public Stream<Person> getUniquelyNamedPeople() {
* @return a Stream of respective
*/ //TODO
public Stream<Person> getUniquelyNamedPeopleStartingWith(Character character) {
return null;
return people.stream().filter(name -> (name.getName().startsWith(String.valueOf(character))));
}

/**
* @param n first `n` Person objects
* @return a Stream of respective
*/ //TODO
public Stream<Person> getFirstNUniquelyNamedPeople(int n) {
return null;
return getUniquelyNamedPeople().limit(n);
}

/**
* @return a mapping of Person Id to the respective Person name
*/ // TODO
public Map<Long, String> getIdToNameMap() {
return null;
return people.stream().collect(Collectors.toMap(Person::getPersonalId, Person::getName));
//creates a map that has a key as personal id and a value as a name
}


/**
* @return Stream of Stream of Aliases
*/ // TODO
public Stream<Stream<String>> getNestedAliases() {
return null;
return people.stream().map(name -> Stream.of(name.getAliases()));
}


/**
* @return Stream of all Aliases
*/ // TODO
public Stream<String> getAllAliases() {
return null;
return getNestedAliases().flatMap(Function.identity());
//look into further to understand exactly how this works.
}

// DO NOT MODIFY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.zipcodewilmington.streams.anthropoid.Person;
import com.zipcodewilmington.streams.anthropoid.PersonFactory;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -25,12 +26,12 @@ public ArrayConverter(int collectionSize) {

//TODO
public List<Person> toList() {
return null;
return Arrays.asList(super.objectSequence);
}

//TODO
public Stream<Person> toStream() {
return null;
return Arrays.stream(super.objectSequence);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.zipcodewilmington.streams.anthropoid.Person;
import com.zipcodewilmington.streams.anthropoid.PersonFactory;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -29,11 +30,11 @@ public List<Person> toList() {

//TODO
public Stream<Person> toStream() {
return null;
return Arrays.stream(super.objectSequence.toArray(new Person[0]));
}

//TODO
public Person[] toArray() {
return null;
return toList().toArray(new Person[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public StreamConverter(int collectionSize) {

// TODO
public List<Person> toList() {
return null;
return personList;
}

// TODO
public Stream<Person> toStream() {
return null;
return personList.stream();
}

// TODO
public Person[] toArray() {
return null;
return personList.toArray(new Person[0]);
}
}