Skip to content
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
2 changes: 2 additions & 0 deletions src/com/walking/lesson57_stream_collect_collector/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.walking.lesson57_stream_collect_collector;

import java.util.stream.Stream;

/**
* Необходимо реализовать набор функций для HRM-системы компании,
* каждая из которых сводится к формированию определенной статистики на базе информации
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,28 @@ public class Department {
private String name;
private int vacancyAmount;
private List<Employee> employees;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getVacancyAmount() {
return vacancyAmount;
}

public void setVacancyAmount(int vacancyAmount) {
this.vacancyAmount = vacancyAmount;
}

public List<Employee> getEmployees() {
return employees;
}

public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
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
Expand Up @@ -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()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вложенные цепочки вызовов обычно усложняют читаемость кода. Вполне можно заменить на map+flatMap

.stream())
.distinct()
Copy link
Owner

Choose a reason for hiding this comment

The 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
Expand Up @@ -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())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

то же, что и выше, дальше подобное не буду дублировать

.distinct()
Copy link
Owner

Choose a reason for hiding this comment

The 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
Expand Up @@ -5,6 +5,9 @@

import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* Предоставьте список сотрудников по каждому имени.
Expand All @@ -13,6 +16,10 @@
public class Task11 implements StatisticTask<SortedMap<String, List<Employee>>> {
@Override
public SortedMap<String, List<Employee>> calculate(List<Department> departments) {
return null;
return departments.stream()
.flatMap(d -> d.getEmployees()
.stream())
.distinct()
.collect(Collectors.groupingBy(Employee::getName, TreeMap::new, Collectors.toList()));
}
}
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))));
Copy link
Owner

Choose a reason for hiding this comment

The 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(
Copy link
Owner

Choose a reason for hiding this comment

The 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,9 +1,11 @@
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;

/**
* По каждой должности (position) предоставьте список сотрудников,
Expand All @@ -12,6 +14,11 @@
public class Task16 implements StatisticTask<Map<String, List<String>>> {
@Override
public Map<String, List<String>> calculate(List<Department> departments) {
return null;
return departments.stream()
.flatMap(d -> d.getEmployees()
.stream())
.distinct()
.collect(Collectors.groupingBy(Employee::getPosition,
Collectors.mapping(Employee::getName, Collectors.toList())));
}
}
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
Expand Up @@ -5,13 +5,18 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Предоставьте список сотрудников женского пола и сотрудников мужского пола в компании.
*/
public class Task18 implements StatisticTask<Map<Boolean, List<Employee>>> {
@Override
public Map<Boolean, List<Employee>> calculate(List<Department> departments) {
return null;
return departments.stream()
.flatMap(d -> d.getEmployees()
.stream())
.distinct()
.collect(Collectors.partitioningBy(Employee::isMale));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Предоставьте список сотрудников женского пола и сотрудников мужского пола по каждому департаменту.
*/
public class Task19 implements StatisticTask<Map<String, Map<Boolean, List<Employee>>>> {
@Override
public Map<String, Map<Boolean, List<Employee>>> calculate(List<Department> departments) {
return null;
return departments.stream()
.collect(Collectors.groupingBy(Department::getName, Collectors.flatMapping(
d -> d.getEmployees()
.stream(), Collectors.partitioningBy(Employee::isMale))));
}
}
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());
}
}
Loading