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>
32 changes: 24 additions & 8 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 @@ -15,28 +16,34 @@
public class StreamFilter {
private final Stream<Person> personStream;
public final String startingCharacter;

public final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/**
* 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 = Stream.generate(new PersonFactory()::createRandomPerson).limit(100);
this.startingCharacter = randomLetter();
}

public String randomLetter() {
Integer index = (int) Math.round(Math.random() * 26);
return this.alphabet.substring(index, index+1);
}

/**
* @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 +62,11 @@ 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(person -> {
String nameChar = person.getName().substring(0,1);
Boolean cool = nameChar.equalsIgnoreCase(startingCharacter);
return cool;
}).collect(Collectors.toList());
}


Expand All @@ -64,7 +75,9 @@ public List<Person> toListMultiLine() {
* @return a list of person objects whose name starts with `this.startingCharacter`
*/ //TODO
public List<Person> toListOneLine() {
return null;
return this.personStream
.filter(person -> person.getName().substring(0,1).equalsIgnoreCase(startingCharacter))
.collect(Collectors.toList());
}


Expand All @@ -73,7 +86,10 @@ public List<Person> toListOneLine() {
* @return an array of person object whose name starts with `this.startingCharacter`
*/ //TODO
public Person[] toArrayOneLine() {
return null;
return this.personStream
.filter(person -> person.getName().substring(0,1).equalsIgnoreCase(startingCharacter))
.collect(Collectors.toList())
.toArray(new Person[0]);
}


Expand All @@ -82,7 +98,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]);
}

}
11 changes: 8 additions & 3 deletions src/main/java/com/zipcodewilmington/streams/StreamMap.java
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.conversions.StreamConverter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -18,22 +19,26 @@ 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;
// ArrayList<Stream<String>> out = new ArrayList<>();
// Arrays.stream(someWords).forEach(someWord -> out.add(letters(someWord)));
// return out.stream();
return Arrays.stream(someWords).map(someWord -> letters(someWord));

}

/**
* @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(someWord -> letters(someWord));
}
}
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,7 @@ public Person createRandomPerson() {
* @return - ArrayList of Person objects
*/ // TODO
public List<Person> createPersonList(int listSize) {
return null;
return createPersonStream(listSize).collect(Collectors.toList());
}


Expand All @@ -48,7 +50,7 @@ public List<Person> createPersonList(int listSize) {
* @return - Array of Person objects
*/ // TODO
public Person[] createPersonArray(int arrayLength) {
return null;
return createPersonStream(arrayLength).collect(Collectors.toList()).toArray(new Person[arrayLength]);
}


Expand All @@ -59,6 +61,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(new PersonFactory()::createRandomPerson).limit(streamCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -36,15 +37,25 @@ 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());
}

public <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Set<Object> seen = ConcurrentHashMap.newKeySet();
return t -> seen.add(keyExtractor.apply(t));
}


/**
* @return list of uniquely named Person objects
* this method is not looking for unique names, but rather the first occurrence of each name
*/ //TODO
public Stream<Person> getUniquelyNamedPeople() {
return null;
return people.stream().filter(distinctByKey(Person::getName));
}

public Boolean checkUniqueName(String name) {
return getNames().stream().filter(n -> n.equals(name)).count() == Long.valueOf(1);
}


Expand All @@ -53,38 +64,42 @@ public Stream<Person> getUniquelyNamedPeople() {
* @return a Stream of respective
*/ //TODO
public Stream<Person> getUniquelyNamedPeopleStartingWith(Character character) {
return null;
return getUniquelyNamedPeople().filter(person -> person.getName().charAt(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;
Map<Long, String> map = new HashMap<>();
people.forEach(person -> map.put(person.getPersonalId(), person.getName()));
return map;
}


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


/**
* @return Stream of all Aliases
*/ // TODO
public Stream<String> getAllAliases() {
return null;
ArrayList<String> actual = new ArrayList<>();
getNestedAliases().forEach(stream -> actual.addAll(stream.collect(Collectors.toList())));
return actual.stream();
}

// DO NOT MODIFY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public ArrayConverter(int collectionSize) {

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

//TODO
public Stream<Person> toStream() {
return null;
return Arrays.stream(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 this.toList().stream();
}

//TODO
public Person[] toArray() {
return null;
return this.toStream().collect(Collectors.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 this.toStream().collect(Collectors.toList());
}

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

// TODO
public Person[] toArray() {
return null;
return this.toList().toArray(new Person[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,25 @@ public void testGetNames() {

Assert.assertEquals(localNames.toString(), warehouseNames.toString());
}

@Test
public void testCheckUniqueName() {
warehouse.addPerson(new Person("Johny", true, 23L, new Date(), new String[0]));
Assert.assertTrue(this.warehouse.checkUniqueName("Johny"));
}

@Test
public void testGetNestedAliases() {
ArrayList<String> actual = new ArrayList<>();
for (Stream<String> streamer : warehouse.getNestedAliases().collect(Collectors.toList())) {
actual.addAll(streamer.collect(Collectors.toList()));
}
Assert.assertEquals(49995, actual.size());
}

// @Test
// public void testGetAllAliases() {
// ArrayList<String> actual = warehouse.getAllAliases().collect(Collectors.toList());
// Assert.assertEquals(49995, actual.size());
// }
}