Skip to content
Open

done #21

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>
26 changes: 18 additions & 8 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,26 @@ 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(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);
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 +60,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;
return personStream.filter(x->x.getName().startsWith(startingCharacter)).collect(Collectors.toList());

}


Expand All @@ -64,16 +70,20 @@ 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().startsWith(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;
public Person[] toArrayOneLine()
{
return personStream.filter(x->x.getName().startsWith(startingCharacter)).toArray(Person[]::new);

}


Expand All @@ -82,7 +92,7 @@ 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().startsWith(startingCharacter)).toArray(Person[]::new);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.zipcodewilmington.streams.anthropoid;

import com.sun.codemodel.internal.JForEach;
import com.zipcodewilmington.streams.tools.RandomUtils;
import com.zipcodewilmington.streams.tools.StringUtils;

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

/**
Expand Down Expand Up @@ -39,16 +40,27 @@ public Person createRandomPerson() {
* @return - ArrayList of Person objects
*/ // TODO
public List<Person> createPersonList(int listSize) {
return null;
}
/* ArrayList<Person> personList = new ArrayList<>(listSize);
for (int i = 0; i < listSize; i++) {
personList.add(createRandomPerson());
}


return personList;*/
return Stream.generate(this::createRandomPerson).limit(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;
/* Person[] personArray = new Person[arrayLength];
for (int i = 0; i < arrayLength; i++) {
personArray[i] = createRandomPerson();
}
return personArray;*/
return Stream.generate(this::createRandomPerson).limit(arrayLength).toArray(Person[]::new);
}


Expand All @@ -59,6 +71,9 @@ 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 = Arrays.stream(createPersonArray(streamCount));
// return personStream;
//Another method
return Stream.generate(this::createRandomPerson).limit(streamCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
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.Collectors;
import java.util.stream.Stream;



/**
* Created by leon on 5/29/17.
* The warehouse is responsible for storing, retrieving, and filtering personSequence
Expand All @@ -36,15 +38,25 @@ 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(x -> x.getName()).
collect(Collectors.toList());
return names;


}


/**
* @return list of uniquely named Person objects
*/ //TODO
public Stream<Person> getUniquelyNamedPeople() {
return null;
List<Person> list = people
.stream()
.filter(distinctByKeys(Person::getName))
.collect(Collectors.toList());


return list.stream();
}


Expand All @@ -53,22 +65,40 @@ public Stream<Person> getUniquelyNamedPeople() {
* @return a Stream of respective
*/ //TODO
public Stream<Person> getUniquelyNamedPeopleStartingWith(Character character) {
return null;
Stream<Person> unique = getUniquelyNamedPeople();

return unique.filter(s -> s.getName().startsWith(character.toString()));
}

/**
* @param n first `n` Person objects
* @return a Stream of respective
*/ //TODO
public Stream<Person> getFirstNUniquelyNamedPeople(int n) {
return null;
Stream<Person>unique = getUniquelyNamedPeople();
List<Person>newList = unique.limit(n).collect(Collectors.toList());



return newList.stream();
}

/**
* @return a mapping of Person Id to the respective Person name
*/ // TODO
public Map<Long, String> getIdToNameMap() {
return null;
/* Map<Long,String>idNameMap = people.stream()
.collect(Collectors.groupingBy(
Person::getPersonalId,
Collectors.mapping(
Person::getName, Collectors.toList());


return idNameMap;*/
Map<Long, String> idNameMap = people.stream()
.collect( Collectors.toMap(Person::getPersonalId,
Person::getName) );
return idNameMap;
}


Expand Down Expand Up @@ -106,4 +136,20 @@ public int size() {
public Iterator<Person> iterator() {
return people.iterator();
}
}


private static <T> Predicate<T> distinctByKeys(Function<? super T, ?>... keyExtractors) {
final Map<List<?>, Boolean> seen = new ConcurrentHashMap<>();

return t ->
{
final List<?> keys = Arrays.stream(keyExtractors)
.map(ke -> ke.apply(t))
.collect(Collectors.toList());

return seen.putIfAbsent(keys, Boolean.TRUE) == null;
};
}


}
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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -25,12 +26,14 @@ public ArrayConverter(int collectionSize) {

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

return new ArrayList<>(Arrays.asList(super.objectSequence));
}

//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 @@ -29,11 +29,13 @@ 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]); //this fashion of writing [0] is new to java it just to say that array will be of same size as collection
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/**
* Created by leon on 5/25/17.
*/

public final class StreamConverter extends PersonConversionAgent<Stream<Person>> {
private final List<Person> personList;
public StreamConverter(Stream<Person> people) {
Expand All @@ -25,16 +26,21 @@ public StreamConverter(int collectionSize) {

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

//return super.objectSequence.collect(Collectors.toList());
return personList;
}

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

//return super.objectSequence;
return personList.stream();
}

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

return personList.toArray(new Person[0]);
}
}