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>
17 changes: 10 additions & 7 deletions src/main/java/com/zipcodewilmington/streams/StreamFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.zipcodewilmington.streams.tools.RandomUtils;
import com.zipcodewilmington.streams.tools.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -20,23 +21,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(Stream.generate(PersonFactory::createRandomPerson).limit(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(Arrays.stream(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 +56,8 @@ 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;
Stream<Person> zanJi = personStream.filter(person -> startingCharacter.equals(person.getName().charAt(0)));
return zanJi.collect(Collectors.toList());
}


Expand All @@ -64,7 +66,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 personStream.filter(person -> startingCharacter.equals(person.getName().charAt(0))).collect(Collectors.toList());
}


Expand All @@ -73,7 +75,7 @@ public List<Person> toListOneLine() {
* @return an array of person object whose name starts with `this.startingCharacter`
*/ //TODO
public Person[] toArrayOneLine() {
return null;
return personStream.filter(person -> startingCharacter.equals(person.getName().charAt(0))).toArray(Person[]::new);
}


Expand All @@ -82,7 +84,8 @@ public Person[] toArrayOneLine() {
* @return an array of person object whose name starts with `this.startingCharacter`
*/ //TODO
public Person[] toArrayMultiLine() {
return null;
Stream<Person> zanJi = personStream.filter(person -> startingCharacter.equalsIgnoreCase(person.getName().substring(0,0)));
return zanJi.toArray(Person[]::new);
}

}
7 changes: 4 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,23 @@ public class StreamMap {
* @return - a Stream of single characters
*/ //TODO
public static Stream<String> letters(String someWord) {
return null;
String[] newOne = someWord.split("");
return Stream.of(newOne);
}

/**
* @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 Stream.of(someWords).map(s -> letters(s));
}

/**
* @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 Stream.of(stringArray).flatMap(s -> letters(s));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public PersonFactory() {
/**
* @return a new instance of a person with fields of random values
*/
public Person createRandomPerson() {
public static Person createRandomPerson() {
String name = StringUtils.capitalizeFirstChar(RandomUtils.createString('a', 'e', 3));
String[] aliases = RandomUtils.createStrings('a', 'z', 3, 5);
boolean isMale = RandomUtils.createBoolean(50);
Expand All @@ -38,17 +38,17 @@ public Person createRandomPerson() {
* @param listSize - number of Person objects to create
* @return - ArrayList of Person objects
*/ // TODO
public List<Person> createPersonList(int listSize) {
return null;
public static List<Person> createPersonList(int listSize) {
return createPersonStream(listSize).collect(Collectors.toList());
}


/**
* @param arrayLength - number of Person objects to create
* @return - Array of Person objects
*/ // TODO
public Person[] createPersonArray(int arrayLength) {
return null;
public static Person[] createPersonArray(int arrayLength) {
return createPersonStream(arrayLength).toArray(Person[]::new);
}


Expand All @@ -58,7 +58,7 @@ public Person[] createPersonArray(int arrayLength) {
* @param streamCount - number of Person objects to create
* @return - Stream representation of collection of Person objects
*/ // TODO
public Stream<Person> createPersonStream(int streamCount) {
return null;
public static Stream<Person> createPersonStream(int streamCount) {
return Stream.generate(PersonFactory::createRandomPerson).limit(streamCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
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.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -18,14 +15,14 @@
* @ATTENTION_TO_STUDENTS You are FORBIDDEN from using loops of any sort within the definition of this class.
*/
public final class PersonWarehouse implements Iterable<Person> {
private final LoggerHandler loggerHandler = LoggerWarehouse.getLogger(PersonWarehouse.class);
private final List<Person> people = new ArrayList<>();
private static final LoggerHandler loggerHandler = LoggerWarehouse.getLogger(PersonWarehouse.class);
private static final List<Person> people = new ArrayList<>();

/**
* @param person the Person object to add to the warehouse
* @ATTENTION_TO_STUDENTS You are FORBIDDEN from modifying this method
*/
public void addPerson(Person person) {
public static void addPerson(Person person) {
loggerHandler.disbalePrinting();
loggerHandler.info("Registering a new person object to the person warehouse...");
loggerHandler.info(ReflectionUtils.getFieldMap(person).toString());
Expand All @@ -35,56 +32,58 @@ public void addPerson(Person person) {
/**
* @return list of names of Person objects
*/ // TODO
public List<String> getNames() {
return null;
public static List<String> getNames() {
return people.stream().map(Person::getName).collect(Collectors.toList());
}


/**
* @return list of uniquely named Person objects
*/ //TODO
public Stream<Person> getUniquelyNamedPeople() {
return null;
public static Stream<Person> getUniquelyNamedPeople() {
Map<String, Person> newMap = people.stream().collect(Collectors.toMap(Person::getName, person1 -> person1, (a , b) -> a));
return newMap.values().stream();
}


/**
* @param character starting character of Person objects' name
* @return a Stream of respective
*/ //TODO
public Stream<Person> getUniquelyNamedPeopleStartingWith(Character character) {
return null;
public static Stream<Person> getUniquelyNamedPeopleStartingWith(Character character) {

return getUniquelyNamedPeople().filter(person -> character.equals(person.getName().charAt(0)));
}

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

/**
* @return a mapping of Person Id to the respective Person name
*/ // TODO
public Map<Long, String> getIdToNameMap() {
return null;
public static Map<Long, String> getIdToNameMap() {
return people.parallelStream().collect(Collectors.toMap(Person::getPersonalId, Person::getName));
}


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


/**
* @return Stream of all Aliases
*/ // TODO
public Stream<String> getAllAliases() {
return null;
public static Stream<String> getAllAliases() {
return people.stream().flatMap(person -> Arrays.stream(person.getAliases()));
}

// DO NOT MODIFY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@ public ArrayConverter(Person... people) {
}

public ArrayConverter(int collectionSize) {
this(Stream
.generate(new PersonFactory()::createRandomPerson)
.limit(collectionSize)
.toArray(Person[]::new));
this(PersonFactory.createPersonArray(collectionSize));
// this(Stream
// .generate(new PersonFactory()::createRandomPerson)
// .limit(collectionSize)
// .toArray(Person[]::new));
}

//TODO
public List<Person> toList() {
return null;
return toStream().collect(Collectors.toList());
}

//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 @@ -16,10 +16,11 @@ public ListConverter(List<Person> people) {
}

public ListConverter(int collectionSize) {
this(Stream
.generate(new PersonFactory()::createRandomPerson)
.limit(collectionSize)
.collect(Collectors.toList()));
this(PersonFactory.createPersonList(collectionSize));
// this(Stream
// .generate(new PersonFactory()::createRandomPerson)
// .limit(collectionSize)
// .collect(Collectors.toList()));
}

@Override
Expand All @@ -29,11 +30,11 @@ public List<Person> toList() {

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

//TODO
public Person[] toArray() {
return null;
return toStream().toArray(Person[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@ public StreamConverter(Stream<Person> people) {
}

public StreamConverter(int collectionSize) {
this(Stream
.generate(new PersonFactory()::createRandomPerson)
.limit(collectionSize));
this(PersonFactory.createPersonStream(collectionSize));
// this(Stream
// .generate(new PersonFactory()::createRandomPerson)
// .limit(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 toStream().toArray(Person[]::new);
}
}