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>
41 changes: 28 additions & 13 deletions src/main/java/com/zipcodewilmington/streams/StreamFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,28 @@ 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.personStream = new PersonFactory().createPersonStream(100);
this.startingCharacter = RandomUtils.createCharacter('A','Z').toString();
}

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

this.personStream = Stream.of(people);
this.startingCharacter = startingCharacter.toString();
}

/**
* @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.personStream = people.stream();
this.startingCharacter = startingCharacter.toString();
}


Expand All @@ -53,36 +58,46 @@ public StreamFilter(Stream<Person> people, Character 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 this.personStream
.filter(person -> person.getName()
.startsWith(this.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 toListMultiLine();
}


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

return toArrayMultiLine();
}


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

return this.personStream
.filter(person -> person.getName()
.startsWith(this.startingCharacter))
.toArray(Person[]::new);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.zipcodewilmington.streams.tools.RandomUtils;
import com.zipcodewilmington.streams.tools.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -39,7 +41,9 @@ public Person createRandomPerson() {
* @return - ArrayList of Person objects
*/ // TODO
public List<Person> createPersonList(int listSize) {
return null;
List<Person> result = new ArrayList<>();
result.addAll(Arrays.asList(createPersonArray(listSize)));
return result;
}


Expand All @@ -48,7 +52,9 @@ public List<Person> createPersonList(int listSize) {
* @return - Array of Person objects
*/ // TODO
public Person[] createPersonArray(int arrayLength) {
return null;
Stream<Person> temp = createPersonStream(arrayLength);
Person [] result = temp.toArray(Person[]::new);
return result;
}


Expand All @@ -59,6 +65,7 @@ public Person[] createPersonArray(int arrayLength) {
* @return - Stream representation of collection of Person objects
*/ // TODO
public Stream<Person> createPersonStream(int streamCount) {
return null;
Stream<Person> personStream = Stream.generate(this :: createRandomPerson).limit(streamCount);
return personStream;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
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 javax.imageio.plugins.jpeg.JPEGImageReadParam;
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;

Expand All @@ -34,41 +36,70 @@ public void addPerson(Person person) {

/**
* @return list of names of Person objects
*/ // TODO
*/
public List<String> getNames() {
return null;
List<String> names = people.stream()
.map(person -> person.getName()) // apply the function to each element in a stream
.collect(Collectors.toList());

return names;
}


/**
* @return list of uniquely named Person objects
*/ //TODO
*/
public Stream<Person> getUniquelyNamedPeople() {
return null;

List<Person> uniquelyNamedPeople = people.stream()
.filter(distinctByName(Person::getName))
.collect(Collectors.toList());

return uniquelyNamedPeople.stream();
}

public static <Person> Predicate<Person> distinctByName(Function<? super Person, ?> keyExtractor) {
Set<Object> result = ConcurrentHashMap.newKeySet();
return Person -> result.add(keyExtractor.apply(Person));

}

/**
* @param character starting character of Person objects' name
* @return a Stream of respective
*/ //TODO
*/
public Stream<Person> getUniquelyNamedPeopleStartingWith(Character character) {
return null;
List<Person> uniqueNames = people.stream()
.filter(person -> person.getName().startsWith(character.toString()))
.collect(Collectors.toList());

return uniqueNames.stream();
}

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

List<Person> firstUnique = uniquePeople.collect(Collectors.toList()).stream()
.limit(n)
.collect(Collectors.toList());

return firstUnique.stream();
}

/**
* @return a mapping of Person Id to the respective Person name
*/ // TODO
*/
public Map<Long, String> getIdToNameMap() {
return null;
Map<Long,String> idToNameMap = new HashMap<>();

idToNameMap = people.stream()
.collect(Collectors.toMap(Person::getPersonalId, Person::getName));

return idToNameMap;
}


Expand All @@ -87,6 +118,10 @@ public Stream<String> getAllAliases() {
return null;
}





// DO NOT MODIFY
public Boolean contains(Person p) {
return people.contains(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public ArrayConverter(int collectionSize) {
.toArray(Person[]::new));
}

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

return Arrays.asList(toArray());
}

//TODO
public Stream<Person> toStream() {
return null;

return Arrays.stream(toArray());
}

@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,13 @@ public List<Person> toList() {

//TODO
public Stream<Person> toStream() {
return null;

return toList().stream();
}

//TODO
public Person[] toArray() {
return null;

return toList().toArray(new Person[toList().size()]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ public StreamConverter(int collectionSize) {
.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 toList().toArray(new Person[toList().size()]);
}
}