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

first commit #1

Open
wants to merge 2 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
15 changes: 12 additions & 3 deletions src/main/java/com/github/curriculeon/anthropoid/PersonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.github.curriculeon.tools.RandomUtils;
import com.github.curriculeon.tools.StringUtils;

import java.util.ArrayList;
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 @@ -38,7 +41,9 @@ public Person createRandomPerson() {
* @return - ArrayList of Person objects
*/ // TODO
public List<Person> createPersonList(int listSize) {
return null;

List<Person> people = IntStream.range(0, listSize).mapToObj(i -> createRandomPerson()).collect(Collectors.toList());
return people;
}


Expand All @@ -47,7 +52,11 @@ public List<Person> createPersonList(int listSize) {
* @return - Array of Person objects
*/ // TODO
public Person[] createPersonArray(int arrayLength) {
return null;

Person [] people = new Person[arrayLength];

createPersonList(arrayLength).toArray(people);
return people;
}


Expand All @@ -58,6 +67,6 @@ public Person[] createPersonArray(int arrayLength) {
* @return - Stream representation of collection of Person objects
*/ // TODO
public Stream<Person> createPersonStream(int streamCount) {
return null;
return createPersonList(streamCount).parallelStream();
}
}
101 changes: 90 additions & 11 deletions src/main/java/com/github/curriculeon/anthropoid/PersonWarehouse.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -35,55 +36,86 @@ 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::getName).collect(Collectors.toList());

return names;
}


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

List<Person> uniquelyNamedPeople = new LinkedList<>();

Map<String, List<Person>> peopleByName = people.stream().collect(
Collectors.groupingBy(Person::getName));

peopleByName
.values()
.stream()
//.filter(peopleWithSameName -> peopleWithSameName.size() == 1)
.forEach(
record -> uniquelyNamedPeople.add(record.get(0)));
return uniquelyNamedPeople.parallelStream();
}

public Map<String, List<Person>> test()
{
Map<String, List<Person>> peopleByName = people.stream().collect(
Collectors.groupingBy(Person::getName));
return peopleByName;
}

/**
* @param character starting character of Person objects' name
* @return a Stream of respective
*/ //TODO
public Stream<Person> getUniquelyNamedPeopleStartingWith(Character character) {
return null;

Stream<Person> personStream = getUniquelyNamedPeople();
return personStream.filter(person -> person.getName().startsWith(Character.toString(character)));
}

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

return personStream.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<>();
this.people.stream().forEach(person -> map.put(person.getPersonalId(), person.getName()));
return map;
}


/**
* @return Stream of Stream of Aliases
*/ // TODO
public Stream<Stream<String>> getNestedAliases() {
return null;

List<Stream<String>> lists = new ArrayList<>();
people.stream().forEach(person -> lists.add(Arrays.asList(person.getAliases()).stream()));
return lists.stream();
}


/**
* @return Stream of all Aliases
*/ // TODO
public Stream<String> getAllAliases() {
return null;
List<String> list = new ArrayList<>();
people.stream().flatMap(person -> Stream.of(person.getAliases())).forEach(list::add);
return list.stream();
}

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

public static void main(String [] args)
{
/*String [] aliases = new String[]{"alias1", "alias2"};
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

try {
Person person = new Person("john", true, 1L, format.parse("2000-06-30"), aliases);
Person person1 = new Person("susan", false, 2L, format.parse("2000-05-20"), aliases);
Person person2 = new Person("alice", false, 3L, format.parse("2000-04-20"), aliases);
Person person3 = new Person("eve", false, 4L, format.parse("1999-05-20"), aliases);
Person person4 = new Person("eve", false, 5L, format.parse("1999-05-20"), aliases);


PersonWarehouse people = new PersonWarehouse();
people.addPerson(person);
people.addPerson(person1);
people.addPerson(person2);
people.addPerson(person3);
people.addPerson(person4);

//people.getUniquelyNamedPeople().forEach(p -> System.out.println(p.getName()));
}catch (ParseException pe)
{
pe.printStackTrace();
}*/
PersonWarehouse warehouse;
PersonFactory factory;
factory = new PersonFactory();
warehouse = new PersonWarehouse();

Map<String, List<Person>> map = new LinkedHashMap<>();

Stream
.generate(factory::createRandomPerson)
.limit(9999)
.forEach(warehouse::addPerson);

map = warehouse.test();
Set<String> names = map.keySet();

for(int i = 0; i < map.size(); i++)
if(map.get(names.toArray()[i]).size() > 1)
System.out.println(map.get(names.toArray()[i]));


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -79,7 +80,7 @@ public void testGetUniquelyNamedPeopleStartingWith() {

private List<String> deriveUniqueNames(PersonWarehouse warehouse) {
// derive unique names
List<String> expectedList = new ArrayList<>();
List<String> expectedList = new LinkedList<>();
for (Person person : warehouse) {
String personName = person.getName();
Boolean isUnique = !expectedList.contains(personName);
Expand Down
19 changes: 19 additions & 0 deletions streams-and-lambdas.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: junit:junit:4.13" 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.10.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.10.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.10.2" level="project" />
</component>
</module>