Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished Exercise #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
Expand Down
77 changes: 44 additions & 33 deletions src/main/java/com/github/curriculeon/StreamFilter.java
Original file line number Diff line number Diff line change
@@ -1,84 +1,95 @@
package com.github.curriculeon;

import com.github.curriculeon.anthropoid.Person;
import com.github.curriculeon.tools.RandomUtils;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Created by leon on 5/2/17.
*/
public class StreamFilter {
private final Stream<Person> personStream;
public final String startingCharacter;

public final char startingCharacter;
/**
* 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.empty(), RandomUtils.createCharacter('A', 'Z'));
}

/**
* @param people - Array of person objects
* @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.empty(), startingCharacter);
}

/**
* @param people - List of person objects
* @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(Stream.empty(), startingCharacter);
}


/**
* @param people - Stream of person objects
* @param people - Stream of person objects
* @param startingCharacter - character to filter by
*/ // I took care of the easy constructor (͡° ͜ʖ ͡°)
public StreamFilter(Stream<Person> people, Character startingCharacter) {
this.personStream = people;
this.startingCharacter = startingCharacter.toString();
this.startingCharacter = startingCharacter;
}


/**
* Using multi-line lambda syntax
*
* @return a list of person object whose name starts with `this.startingCharacter`
*/ //TODO
*/
public List<Person> toListMultiLine() {
return null;
return personStream
.filter(person -> {
return person.getName()
.toCharArray()[0] == startingCharacter;
})
.collect(Collectors.toList());
}


/**
* Using one-line lambda syntax
*
* @return a list of person objects whose name starts with `this.startingCharacter`
*/ //TODO
*/
public List<Person> toListOneLine() {
return null;
return personStream.filter(person -> person.getName().toCharArray()[0] == startingCharacter).collect(Collectors.toList());
}


/**
* Using one-line lambda syntax
*
* @return an array of person object whose name starts with `this.startingCharacter`
*/ //TODO
*/
public Person[] toArrayOneLine() {
return null;
return toListOneLine().toArray(Person[]::new);
}


/**
* Using multi-line lambda syntax
*
* @return an array of person object whose name starts with `this.startingCharacter`
*/ //TODO
*/
public Person[] toArrayMultiLine() {
return null;
return toListMultiLine().toArray(Person[]::new);
}

}
13 changes: 7 additions & 6 deletions src/main/java/com/github/curriculeon/StreamMap.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.curriculeon;

import java.util.function.UnaryOperator;
import java.util.stream.Stream;

/**
Expand All @@ -10,24 +11,24 @@ public class StreamMap {
* Section 8.3
* @param someWord - word to convert to Stream<String>
* @return - a Stream of single characters
*/ //TODO
*/
public static Stream<String> letters(String someWord) {
return null;
return Stream.of(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 Stream.of(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 wordsMap().flatMap(UnaryOperator.identity());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -36,18 +38,18 @@ 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;
return IntStream.range(0, listSize).boxed().map(value -> createRandomPerson()).collect(Collectors.toList());
}


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


Expand All @@ -56,8 +58,8 @@ 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;
return createPersonList(streamCount).stream();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import com.github.curriculeon.tools.logging.LoggerWarehouse;
import com.github.curriculeon.tools.ReflectionUtils;

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 @@ -33,57 +31,57 @@ 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
*/ //https://stackoverflow.com/a/49744772
public Stream<Person> getUniquelyNamedPeople() {
return null;
var names = new HashSet<>();
return people.stream().filter(person -> names.add(person.getName()));
}



/**
* @param character starting character of Person objects' name
* @return a Stream of respective
*/ //TODO
public Stream<Person> getUniquelyNamedPeopleStartingWith(Character character) {
return null;
*/
public Stream<Person> getUniquelyNamedPeopleStartingWith(char character) {
return getUniquelyNamedPeople().filter(person -> person.getName().toCharArray()[0] == 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));
}


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


/**
* @return Stream of all Aliases
*/ // TODO
public Stream<String> getAllAliases() {
return null;
return getNestedAliases().flatMap(aliasesStream -> aliasesStream);
}

// DO NOT MODIFY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.curriculeon.anthropoid.PersonFactory;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
Expand All @@ -21,14 +22,12 @@ public ArrayConverter(int collectionSize) {
.toArray(Person[]::new));
}

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

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ public List<Person> toList() {
return super.objectSequence;
}

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

//TODO
public Person[] toArray() {
return null;
return objectSequence.toArray(Person[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,16 @@ public StreamConverter(int collectionSize) {
.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 personList.toArray(Person[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Created by leon on 5/25/17.
* @ATTENTION_TO_STUDENTS You are FORBIDDEN from modifying this class
*/
public class TestConversionAgent<T extends PersonConversionAgent<Person>> {
public abstract class TestConversionAgent<T extends PersonConversionAgent<Person>> {
private final T conversionAgent;

private List<Person> personList;
Expand All @@ -23,7 +23,7 @@ public class TestConversionAgent<T extends PersonConversionAgent<Person>> {
public TestConversionAgent(T conversionAgent) {
this.conversionAgent = conversionAgent;
}

@Before
public void setup() {
this.personStream = conversionAgent.toStream();
Expand Down
Loading