Добавил немного примеров со стримами

This commit is contained in:
Struchkov Mark 2022-07-23 10:41:34 +03:00
parent 90756ccd27
commit a5bc59b03f
2 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,89 @@
package dev.struchkov.example.debugger.seven;
public class Employee {
private String firstName;
private String lastName;
private Integer age;
private Integer salary;
private Position position;
public Employee(String firstName, String lastName, Integer age, Integer salary, Position position) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.salary = salary;
this.position = position;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
@Override
public String toString() {
return position + " " + firstName + " " + lastName + " " + age + " лет, зарплата: " + salary;
}
public static Employee of(String firstName, String lastName, Integer age, Integer salary, Position position) {
return new Employee(firstName, lastName, age, salary, position);
}
public enum Position {
DEV("Разработчик"),
ACCOUNTANT("Бухгалтер"),
BOSS("Начальник отдела");
private final String title;
Position(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
@Override
public String toString() {
return title;
}
}
}

View File

@ -0,0 +1,41 @@
package dev.struchkov.example.debugger.seven;
import dev.struchkov.example.debugger.seven.Employee.Position;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static dev.struchkov.example.debugger.seven.Employee.Position.ACCOUNTANT;
import static dev.struchkov.example.debugger.seven.Employee.Position.BOSS;
import static dev.struchkov.example.debugger.seven.Employee.Position.DEV;
public class StreamMain {
public static void main(String[] args) {
final List<Employee> employees = List.of(
Employee.of("Екатерина", "Козлова", 28, 2_000_000, BOSS),
Employee.of("Алексей", "Кузнецов", 45, 90_000, ACCOUNTANT),
Employee.of("Юлия", "Козлова", 18, 35_000, ACCOUNTANT),
Employee.of("Никита", "Васильев", 22, 110_000, DEV),
Employee.of("Иван", "Иванов", 30, 300_000, DEV),
Employee.of("Анна", "Ермолова", 45, null, DEV),
Employee.of("Ирина", "Колесникова", 35, 200_000, DEV)
);
final Set<String> devs = employees.stream()
.filter(employee -> DEV.equals(employee.getPosition()))
.filter(employee -> employee.getSalary() != null)
.filter(employee -> employee.getSalary() > 150_000)
.map(Employee::getLastName)
.collect(Collectors.toSet());
final Map<Position, List<Employee>> collect = employees.stream()
.filter(employee -> employee.getSalary() != null)
.collect(Collectors.groupingBy(Employee::getPosition));
}
}