-
Notifications
You must be signed in to change notification settings - Fork 51
Streams completed #57
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
Open
nikitos19
wants to merge
1
commit into
java8-course:master
Choose a base branch
from
nikitos19:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,14 @@ | |
import data.Person; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static data.Generator.generateEmployeeList; | ||
|
@@ -22,102 +26,165 @@ public class StreamsExercise2 { | |
// https://youtu.be/O8oN4KSZEXE Сергей Куксенко — Stream API, часть 1 | ||
// https://youtu.be/i0Jr2l3jrDA Сергей Куксенко — Stream API, часть 2 | ||
|
||
// TODO class PersonEmployerPair | ||
private static class PersonEmployerPair { | ||
|
||
private final Person person; | ||
private final String employer; | ||
private final int duration; | ||
|
||
private PersonEmployerPair(Person person, String employer, int duration) { | ||
this.person = person; | ||
this.employer = employer; | ||
this.duration = duration; | ||
} | ||
|
||
public Person getPerson() { | ||
return person; | ||
} | ||
|
||
public String getEmployer() { | ||
return employer; | ||
} | ||
|
||
public int getDuration() { | ||
return duration; | ||
} | ||
} | ||
|
||
@Test | ||
public void employersStuffLists() { | ||
Map<String, List<Person>> employersStuffLists = null;// TODO | ||
throw new UnsupportedOperationException(); | ||
Map<String, List<Person>> employersStuffLists = getEmployees().stream() | ||
.flatMap(employee -> employee.getJobHistory().stream() | ||
.map(j -> new PersonEmployerPair(employee.getPerson(), j.getEmployer(), j.getDuration()))) | ||
.collect(Collectors.groupingBy( | ||
PersonEmployerPair::getEmployer, | ||
mapping(PersonEmployerPair::getPerson, toList()) | ||
)); | ||
|
||
List<Person> personList = new ArrayList<>(); | ||
personList.add(new Person("John", "Doe", 21)); | ||
personList.add(new Person("John", "Doe", 24)); | ||
personList.add(new Person("Bob", "Doe", 27)); | ||
personList.add(new Person("John", "Doe", 30)); | ||
|
||
assertEquals(personList, employersStuffLists.get("abc")); | ||
} | ||
|
||
@Test | ||
public void indexByFirstEmployer() { | ||
Map<String, List<Person>> employeesIndex = null;// TODO | ||
throw new UnsupportedOperationException(); | ||
Map<String, List<Person>> employeesIndex = getEmployees().stream() | ||
.map(employee -> new PersonEmployerPair(employee.getPerson(), | ||
employee.getJobHistory().get(0).getEmployer(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid |
||
employee.getJobHistory().get(0).getDuration())) | ||
.collect(Collectors.groupingBy( | ||
PersonEmployerPair::getEmployer, | ||
mapping(PersonEmployerPair::getPerson, toList()) | ||
)); | ||
|
||
List<Person> personList = new ArrayList<>(); | ||
personList.add(new Person("John", "Galt", 20)); | ||
personList.add(new Person("John", "White", 22)); | ||
personList.add(new Person("John", "Galt", 23)); | ||
personList.add(new Person("John", "White", 25)); | ||
personList.add(new Person("John", "Galt", 26)); | ||
personList.add(new Person("John", "White", 28)); | ||
personList.add(new Person("John", "Galt", 29)); | ||
personList.add(new Person("Bob", "White", 31)); | ||
|
||
assertEquals(personList, employeesIndex.get("epam")); | ||
|
||
} | ||
|
||
@Test | ||
public void greatestExperiencePerEmployer() { | ||
Map<String, Person> employeesIndex = null;// TODO | ||
Map<String, Person> employeesIndex = getEmployees().stream() | ||
.flatMap(employee -> employee.getJobHistory().stream() | ||
.map(j -> new PersonEmployerPair(employee.getPerson(), j.getEmployer(), j.getDuration()))) | ||
.collect(Collectors.groupingBy( | ||
PersonEmployerPair::getEmployer, | ||
Collectors.collectingAndThen( | ||
Collectors.maxBy(Comparator.comparingInt(PersonEmployerPair::getDuration)), | ||
p -> p.get().getPerson()) | ||
)); | ||
|
||
assertEquals(new Person("John", "White", 28), employeesIndex.get("epam")); | ||
} | ||
|
||
|
||
private List<Employee> getEmployees() { | ||
return Arrays.asList( | ||
new Employee( | ||
new Person("John", "Galt", 20), | ||
Arrays.asList( | ||
new JobHistoryEntry(3, "dev", "epam"), | ||
new JobHistoryEntry(2, "dev", "google") | ||
)), | ||
new Employee( | ||
new Person("John", "Doe", 21), | ||
Arrays.asList( | ||
new JobHistoryEntry(4, "BA", "yandex"), | ||
new JobHistoryEntry(2, "QA", "epam"), | ||
new JobHistoryEntry(2, "dev", "abc") | ||
)), | ||
new Employee( | ||
new Person("John", "White", 22), | ||
Collections.singletonList( | ||
new JobHistoryEntry(6, "QA", "epam") | ||
)), | ||
new Employee( | ||
new Person("John", "Galt", 23), | ||
Arrays.asList( | ||
new JobHistoryEntry(3, "dev", "epam"), | ||
new JobHistoryEntry(2, "dev", "google") | ||
)), | ||
new Employee( | ||
new Person("John", "Doe", 24), | ||
Arrays.asList( | ||
new JobHistoryEntry(4, "QA", "yandex"), | ||
new JobHistoryEntry(2, "BA", "epam"), | ||
new JobHistoryEntry(2, "dev", "abc") | ||
)), | ||
new Employee( | ||
new Person("John", "White", 25), | ||
Collections.singletonList( | ||
new JobHistoryEntry(6, "QA", "epam") | ||
)), | ||
new Employee( | ||
new Person("John", "Galt", 26), | ||
Arrays.asList( | ||
new JobHistoryEntry(3, "dev", "epam"), | ||
new JobHistoryEntry(1, "dev", "google") | ||
)), | ||
new Employee( | ||
new Person("Bob", "Doe", 27), | ||
Arrays.asList( | ||
new JobHistoryEntry(4, "QA", "yandex"), | ||
new JobHistoryEntry(2, "QA", "epam"), | ||
new JobHistoryEntry(2, "dev", "abc") | ||
)), | ||
new Employee( | ||
new Person("John", "White", 28), | ||
Collections.singletonList( | ||
new JobHistoryEntry(666, "BA", "epam") | ||
)), | ||
new Employee( | ||
new Person("John", "Galt", 29), | ||
Arrays.asList( | ||
new JobHistoryEntry(3, "dev", "epam"), | ||
new JobHistoryEntry(1, "dev", "google") | ||
)), | ||
new Employee( | ||
new Person("John", "Doe", 30), | ||
Arrays.asList( | ||
new JobHistoryEntry(4, "QA", "yandex"), | ||
new JobHistoryEntry(2, "QA", "epam"), | ||
new JobHistoryEntry(5, "dev", "abc") | ||
)), | ||
new Employee( | ||
new Person("Bob", "White", 31), | ||
Collections.singletonList( | ||
new JobHistoryEntry(6, "QA", "epam") | ||
)) | ||
new Employee( | ||
new Person("John", "Galt", 20), | ||
Arrays.asList( | ||
new JobHistoryEntry(3, "dev", "epam"), | ||
new JobHistoryEntry(2, "dev", "google") | ||
)), | ||
new Employee( | ||
new Person("John", "Doe", 21), | ||
Arrays.asList( | ||
new JobHistoryEntry(4, "BA", "yandex"), | ||
new JobHistoryEntry(2, "QA", "epam"), | ||
new JobHistoryEntry(2, "dev", "abc") | ||
)), | ||
new Employee( | ||
new Person("John", "White", 22), | ||
Collections.singletonList( | ||
new JobHistoryEntry(6, "QA", "epam") | ||
)), | ||
new Employee( | ||
new Person("John", "Galt", 23), | ||
Arrays.asList( | ||
new JobHistoryEntry(3, "dev", "epam"), | ||
new JobHistoryEntry(2, "dev", "google") | ||
)), | ||
new Employee( | ||
new Person("John", "Doe", 24), | ||
Arrays.asList( | ||
new JobHistoryEntry(4, "QA", "yandex"), | ||
new JobHistoryEntry(2, "BA", "epam"), | ||
new JobHistoryEntry(2, "dev", "abc") | ||
)), | ||
new Employee( | ||
new Person("John", "White", 25), | ||
Collections.singletonList( | ||
new JobHistoryEntry(6, "QA", "epam") | ||
)), | ||
new Employee( | ||
new Person("John", "Galt", 26), | ||
Arrays.asList( | ||
new JobHistoryEntry(3, "dev", "epam"), | ||
new JobHistoryEntry(1, "dev", "google") | ||
)), | ||
new Employee( | ||
new Person("Bob", "Doe", 27), | ||
Arrays.asList( | ||
new JobHistoryEntry(4, "QA", "yandex"), | ||
new JobHistoryEntry(2, "QA", "epam"), | ||
new JobHistoryEntry(2, "dev", "abc") | ||
)), | ||
new Employee( | ||
new Person("John", "White", 28), | ||
Collections.singletonList( | ||
new JobHistoryEntry(666, "BA", "epam") | ||
)), | ||
new Employee( | ||
new Person("John", "Galt", 29), | ||
Arrays.asList( | ||
new JobHistoryEntry(3, "dev", "epam"), | ||
new JobHistoryEntry(1, "dev", "google") | ||
)), | ||
new Employee( | ||
new Person("John", "Doe", 30), | ||
Arrays.asList( | ||
new JobHistoryEntry(4, "QA", "yandex"), | ||
new JobHistoryEntry(2, "QA", "epam"), | ||
new JobHistoryEntry(5, "dev", "abc") | ||
)), | ||
new Employee( | ||
new Person("Bob", "White", 31), | ||
Collections.singletonList( | ||
new JobHistoryEntry(6, "QA", "epam") | ||
)) | ||
); | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use
get
- it fails on empty collection.