-
Notifications
You must be signed in to change notification settings - Fork 103
Lesson 57 (stream collect collector) #103
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
base: for-pr
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,64 @@ | ||
| package com.walking.lesson57_stream_collect_collector.model; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class Employee { | ||
| private String name; | ||
| private boolean isMale; | ||
| private String position; | ||
| private int age; | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public boolean isMale() { | ||
| return isMale; | ||
| } | ||
|
|
||
| public void setMale(boolean male) { | ||
| isMale = male; | ||
| } | ||
|
|
||
| public String getPosition() { | ||
| return position; | ||
| } | ||
|
|
||
| public void setPosition(String position) { | ||
| this.position = position; | ||
| } | ||
|
|
||
| public int getAge() { | ||
| return age; | ||
| } | ||
|
|
||
| public void setAge(int age) { | ||
| this.age = age; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
|
|
||
| Employee employee = (Employee) o; | ||
| return isMale == employee.isMale && age == employee.age && Objects.equals(name, | ||
| employee.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int result = Objects.hashCode(name); | ||
| result = 31 * result + Boolean.hashCode(isMale); | ||
| result = 31 * result + age; | ||
| return result; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,18 @@ | |
| import com.walking.lesson57_stream_collect_collector.model.Employee; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Предоставьте список всех сотрудников компании. | ||
| */ | ||
| public class Task1 implements StatisticTask<List<Employee>> { | ||
| @Override | ||
| public List<Employee> calculate(List<Department> departments) { | ||
| return null; | ||
| return departments.stream() | ||
| .flatMap(d -> d.getEmployees() | ||
| .stream()) | ||
| .distinct() | ||
|
Owner
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. зачем? |
||
| .collect(Collectors.toList()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,13 +5,18 @@ | |
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Предоставьте список сотрудников по каждому имени. | ||
| */ | ||
| public class Task10 implements StatisticTask<Map<String, List<Employee>>> { | ||
| @Override | ||
| public Map<String, List<Employee>> calculate(List<Department> departments) { | ||
| return null; | ||
| return departments.stream() | ||
| .flatMap(d -> d.getEmployees() | ||
| .stream()) | ||
|
Owner
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. то же, что и выше, дальше подобное не буду дублировать |
||
| .distinct() | ||
|
Owner
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. То же, что и выше |
||
| .collect(Collectors.groupingBy(Employee::getName)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,22 @@ | ||
| package com.walking.lesson57_stream_collect_collector.task; | ||
|
|
||
| import com.walking.lesson57_stream_collect_collector.model.Department; | ||
| import com.walking.lesson57_stream_collect_collector.model.Employee; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Предоставьте количество сотрудников старше 50 по каждому департаменту. | ||
| */ | ||
| public class Task12 implements StatisticTask<Map<String, Long>> { | ||
| @Override | ||
| public Map<String, Long> calculate(List<Department> departments) { | ||
| return null; | ||
| return departments.stream() | ||
| .collect(Collectors.groupingBy(Department::getName, Collectors.flatMapping( | ||
| d -> d.getEmployees() | ||
| .stream() | ||
| .filter(e -> e.getAge() > 50), Collectors.counting()))); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,22 @@ | ||
| package com.walking.lesson57_stream_collect_collector.task; | ||
|
|
||
| import com.walking.lesson57_stream_collect_collector.model.Department; | ||
| import com.walking.lesson57_stream_collect_collector.model.Employee; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Предоставьте информацию о среднем возрасте сотрудников компании. | ||
| */ | ||
| public class Task13 implements StatisticTask<Double> { | ||
| @Override | ||
| public Double calculate(List<Department> departments) { | ||
| return null; | ||
| return departments.stream() | ||
| .flatMap(d -> d.getEmployees() | ||
| .stream()) | ||
| .distinct() | ||
| .collect(Collectors.averagingDouble(Employee::getAge)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,21 @@ | ||
| package com.walking.lesson57_stream_collect_collector.task; | ||
|
|
||
| import com.walking.lesson57_stream_collect_collector.model.Department; | ||
| import com.walking.lesson57_stream_collect_collector.model.Employee; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Предоставьте информацию о среднем возрасте сотрудников по каждому департаменту. | ||
| */ | ||
| public class Task14 implements StatisticTask<Map<String, Double>> { | ||
| @Override | ||
| public Map<String, Double> calculate(List<Department> departments) { | ||
| return null; | ||
| return departments.stream() | ||
| .collect(Collectors.groupingBy(Department::getName, Collectors.flatMapping( | ||
| d -> d.getEmployees() | ||
| .stream(), Collectors.averagingDouble(Employee::getAge)))); | ||
|
Owner
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. я очень большой сторонник правила точка-строчка, но когда стрим или другая цепочка - не единственный параметр метода, смотрится такая конструкция мутно |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,26 @@ | ||
| package com.walking.lesson57_stream_collect_collector.task; | ||
|
|
||
| import com.walking.lesson57_stream_collect_collector.model.Department; | ||
| import com.walking.lesson57_stream_collect_collector.model.Employee; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Предоставьте соотношение женщин и мужчин по каждому департаменту. | ||
| */ | ||
| public class Task15 implements StatisticTask<Map<String, Double>> { | ||
| @Override | ||
| public Map<String, Double> calculate(List<Department> departments) { | ||
| return null; | ||
| return departments.stream() | ||
| .collect(Collectors.groupingBy(Department::getName, Collectors.teeing(Collectors.flatMapping( | ||
| d -> d.getEmployees() | ||
| .stream() | ||
| .filter(e -> !e.isMale()), Collectors.counting()), Collectors.flatMapping( | ||
|
Owner
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. Collectors.flatMapping( логичнее бы на след строку перенести. Как и предыдущий |
||
| d -> d.getEmployees() | ||
| .stream() | ||
| .filter(Employee::isMale), Collectors.counting()), | ||
| (womenCount, manCount) -> (double) womenCount / manCount))); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,26 @@ | ||
| package com.walking.lesson57_stream_collect_collector.task; | ||
|
|
||
| import com.walking.lesson57_stream_collect_collector.model.Department; | ||
| import com.walking.lesson57_stream_collect_collector.model.Employee; | ||
|
|
||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Предоставьте информацию по максимальному возрасту сотрудников в каждом из департаментов. | ||
| */ | ||
| public class Task17 implements StatisticTask<Map<String, Integer>> { | ||
| @Override | ||
| public Map<String, Integer> calculate(List<Department> departments) { | ||
| return null; | ||
| return departments.stream() | ||
| .collect(Collectors.toMap(Department::getName, d -> d.getEmployees() | ||
| .stream() | ||
| .mapToInt(Employee::getAge) | ||
| .max() | ||
| .orElse(0))); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,22 @@ | ||
| package com.walking.lesson57_stream_collect_collector.task; | ||
|
|
||
| import com.walking.lesson57_stream_collect_collector.model.Department; | ||
| import com.walking.lesson57_stream_collect_collector.model.Employee; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Предоставьте полный перечень оригинальных имен сотрудников компании. | ||
| */ | ||
| public class Task2 implements StatisticTask<Set<String>> { | ||
| @Override | ||
| public Set<String> calculate(List<Department> departments) { | ||
| return null; | ||
| return departments.stream() | ||
| .flatMap(d -> d.getEmployees() | ||
| .stream()) | ||
| .map(Employee::getName) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
| } |
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.
Вложенные цепочки вызовов обычно усложняют читаемость кода. Вполне можно заменить на map+flatMap