-
Notifications
You must be signed in to change notification settings - Fork 50
streams done #54
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
jinstrm
wants to merge
9
commits into
java8-course:master
Choose a base branch
from
jinstrm: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
streams done #54
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5fa7d71
StreamExersise1 1 of 3 tests done
9813c0b
StreamExersise1 3 of 3 tests done
41b762e
part 1 StreamExersise1 3 of 3 tests done
ef782c2
part 2 CollectorsExercise1 3 of 4 completed
50a5fe5
part 2 CollectorsExercise1 4 of 4 completed
f7d40df
part2 CollectorsExercise2 1.5 of 3 completed
d14a655
part2 CollectorsExercise2 2 of 3 completed
28cb0e9
part3 streams done
cc9dd21
part3 CollectorsCombo done
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
|
|
||
| import java.util.*; | ||
| import java.util.concurrent.ThreadLocalRandom; | ||
| import java.util.function.Predicate; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
| import java.util.stream.Stream; | ||
|
|
@@ -15,6 +16,9 @@ | |
| import static java.util.stream.Collectors.groupingBy; | ||
| import static java.util.stream.Collectors.mapping; | ||
| import static java.util.stream.Collectors.toList; | ||
| import static org.hamcrest.core.IsEqual.equalTo; | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertThat; | ||
|
|
||
| public class StreamsExercise1 { | ||
| // https://youtu.be/kxgo7Y4cdA8 Сергей Куксенко и Алексей Шипилёв — Через тернии к лямбдам, часть 1 | ||
|
|
@@ -25,16 +29,60 @@ public class StreamsExercise1 { | |
|
|
||
| @Test | ||
| public void getAllEpamEmployees() { | ||
| List<Person> epamEmployees = null;// TODO all persons with experience in epam | ||
| throw new UnsupportedOperationException(); | ||
| final List<Employee> employees = generateEmployeeList(); | ||
|
|
||
| List<Person> expected = new ArrayList<>(); | ||
|
|
||
| for (Employee e : employees){ | ||
| for (JobHistoryEntry j : e.getJobHistory()) | ||
| if (j.getEmployer().equals("epam")){ | ||
| expected.add(e.getPerson()); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| List<Person> actual = employees.stream() | ||
| .filter(this::hasExperienceInEpam) | ||
| .map(Employee::getPerson) | ||
| .collect(toList()); | ||
|
|
||
|
|
||
| assertThat(actual, equalTo(expected)); | ||
| } | ||
|
|
||
| private boolean hasExperienceInEpam(Employee employee){ | ||
| return employee.getJobHistory().stream() | ||
| .filter(j -> j.getEmployer().equals("epam")) | ||
| .count() > 0; | ||
| } | ||
|
|
||
| @Test | ||
| public void getEmployeesStartedFromEpam() { | ||
| List<Person> epamEmployees = null;// TODO all persons with first experience in epam | ||
| throw new UnsupportedOperationException(); | ||
| final List<Employee> employees = generateEmployeeList(); | ||
|
|
||
| List<Person> expected = new ArrayList<>(); | ||
|
|
||
| for (Employee e : employees){ | ||
| if (e.getJobHistory().size() > 0 && e.getJobHistory().get(0).getEmployer().equals("epam")) | ||
| expected.add(e.getPerson()); | ||
| } | ||
|
|
||
| List<Person> actual = employees.stream() | ||
| .filter(this::startExperienceInEpam) | ||
| .map(Employee::getPerson) | ||
| .collect(toList()); | ||
|
|
||
| assertThat(actual, equalTo(expected)); | ||
| } | ||
|
|
||
| private boolean startExperienceInEpam(Employee employee) { | ||
| return employee.getJobHistory().stream() | ||
| .limit(1) | ||
| .filter(j -> j.getEmployer().equals("epam")) | ||
| .count() > 0; | ||
|
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.
|
||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void sumEpamDurations() { | ||
| final List<Employee> employees = generateEmployeeList(); | ||
|
|
@@ -49,11 +97,13 @@ public void sumEpamDurations() { | |
| } | ||
| } | ||
|
|
||
| // TODO | ||
| throw new UnsupportedOperationException(); | ||
|
|
||
| // int result = ??? | ||
| // assertEquals(expected, result); | ||
| int result = employees.stream() | ||
| .flatMap(employee -> employee.getJobHistory().stream()) | ||
| .filter(j -> j.getEmployer().equals("epam")) | ||
| .mapToInt(JobHistoryEntry::getDuration) | ||
| .sum(); | ||
|
|
||
| assertEquals(expected, result); | ||
| } | ||
|
|
||
| } | ||
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
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.
Use
anyMatchinstead.