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
19 changes: 0 additions & 19 deletions looptest.iml

This file was deleted.

45 changes: 38 additions & 7 deletions src/main/java/com/zipcodewilmington/streams/StreamFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.zipcodewilmington.streams.tools.RandomUtils;
import com.zipcodewilmington.streams.tools.StringUtils;

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

Expand All @@ -20,23 +22,32 @@ 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);

//ONLY CAN HAVE STATIC METHODS CALLED IN THE CONSTRUCTOR because it creates the object. We can't use the
//methods in the class because we don't yet have an object to call them.

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);

//if we have "this ( Stream.empty(), startingCharacter null)" with parenthesis, we're calling another constructor specifically going to call the
//constructor that has those two parameters,

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 +66,10 @@ 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 personStream.filter(x -> {
boolean b = x.getName().matches(this.startingCharacter);
return b;
}).collect(Collectors.toList());
}


Expand All @@ -64,7 +78,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 personStream.filter(x -> x.getName()
.matches(this.startingCharacter))
.collect(Collectors.toList());
}


Expand All @@ -73,7 +89,8 @@ 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(x -> x.getName().matches(this.startingCharacter))
.toArray(Person[]::new);
}


Expand All @@ -82,7 +99,21 @@ public Person[] toArrayOneLine() {
* @return an array of person object whose name starts with `this.startingCharacter`
*/ //TODO
public Person[] toArrayMultiLine() {
return null;
return personStream.filter(x -> x.getName().matches(this.startingCharacter))
.toArray(Person[]::new);
}

public Character createRandomLetter () {
// Random rnd = new Random();
// char c = (char) (rnd.nextInt(26) + 'a');

// Stream.generate(Math::random)
// .limit(limitTerms)

String chars = "abcdefghijklmnopqrstuvxyz";
Random rnd = new Random();
char c = chars.charAt(rnd.nextInt(chars.length()));
return c;
}

}
14 changes: 11 additions & 3 deletions src/main/java/com/zipcodewilmington/streams/StreamMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -18,22 +19,29 @@ public class StreamMap {
* @return - a Stream of single characters
*/ //TODO
public static Stream<String> letters(String someWord) {
return null;
String [] wordArray = someWord.split("");
return Arrays.stream(wordArray);
}

/**
* @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);

//Basically a stream of a stream is what is wanted. So we take each word, and then apply the letters method from
//this class on those words and return it.
}

/**
* @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(Function.identity());
//Uses the wordsMap function from this class and then returns it as flat map since the return is
//not nested.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public Person createRandomPerson() {
* @return - ArrayList of Person objects
*/ // TODO
public List<Person> createPersonList(int listSize) {
return null;
List <Person> returnedList = createPersonStream(listSize).collect(Collectors.toList());
return returnedList;
}


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


Expand All @@ -59,6 +60,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,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,32 @@ 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;
public Stream <Person> getUniquelyNamedPeople() {
Set<String> mySet = new HashSet<>((people.size()));

return people.stream().filter(p -> mySet.add(p.getName()));

// uniqueNames = people.stream().collect(Collectors.map(Person, getNames())
// .entrySet()
// .stream()
// .filter(entry -> entry.getValue() == 1)
// .map(Map.Entry::getKey);
//// .collect(Collectors.toList());
//
//return people.stream().map(e -> e.getName());
//
//stream.of(linked)
// return uniqueNames;
// return null;
}


Expand All @@ -53,38 +71,60 @@ public Stream<Person> getUniquelyNamedPeople() {
* @return a Stream of respective
*/ //TODO
public Stream<Person> getUniquelyNamedPeopleStartingWith(Character character) {
return null;
return people.stream().filter(thePerson -> (thePerson.getName().indexOf(character) == 0));

// String myChar = String.valueOf(character);
// Predicate<String> predicate = new Predicate <String> (){
//
// @Override
// public boolean test(String o) {
// return (o.startsWith(myChar));
// }
// };
//people.stream().filter(predicate::apply);
}

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

//
// uniqueNames = people.stream().collect(Collectors.map(Person, getNames())
// .entrySet()
// .stream()
// .filter(entry -> entry.getValue() == 1)
// .map(Map.Entry::getKey);
//// .collect(Collectors.toList());
//

/**
* @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;
public Stream<Stream<String>> getNestedAliases()
{
return people.stream().map(x -> Stream.of(x.getAliases()));

}


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

// DO NOT MODIFY
Expand All @@ -106,4 +146,10 @@ public int size() {
public Iterator<Person> iterator() {
return people.iterator();
}

public static <T> Predicate <T> distinctByKey (Function <? super T, Object > keyExtractor) {
Map<Object, Boolean> map = new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE == null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* Created by leon on 5/25/17.
*/
public final class ArrayConverter extends PersonConversionAgent<Person[]> {
Person [] people;

public ArrayConverter(Person... people) {
super(people);
}
Expand All @@ -25,12 +27,15 @@ 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(objectSequence);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public List<Person> toList() {

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

//TODO
public Person[] toArray() {
return null;
return super.objectSequence.toArray(new Person[0]);
}
}
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.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -25,16 +26,18 @@ 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;
Person[] array = new Person[personList.size()];
array = personList.toArray(array);
return array;
}
}