Backend
home
📝

stream 예제 코드 11 - EmployeeSystem

생성일
2025/01/22 10:39
태그
Employee
package com.assignment; import lombok.Data; @Data public class Employee { private int id; private String name; private String department; private double salary; private String email; // 생성자, getter, setter 메소드들을 작성하세요 public Employee(int id, String name, String department, double salary, String email) { this.id = id; this.name = name; this.department = department; this.salary = salary; this.email = email; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
Java
복사
EMSystem.java
package com.assignment; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; public class EMSystem { // 부서별 직원 필터링 - 특정 부서(department)에 속한 직원들을 필터링하여 반환하는 메소드를 작성하세요. public List<Employee> filterByDepartment(List<Employee> employees, String department) { // return employees.stream() // .filter(employee -> department.contains(employee.getDepartment())) // .collect(Collectors.toList()); return employees.stream() .filter(employee -> employee.getDepartment().equals(department)) .toList(); } // 직원 이메일 목록 추출 - 모든 직원의 이메일 주소를 추출하여 리스트로 반환하는 메소드를 작성하세요. public List<String> getEmailAddresses(List<Employee> employees) { return employees.stream() .map(employee -> employee.getEmail()) .collect(Collectors.toList()); } // 직원 급여 합계 계산 - 모든 직원의 급여 합계를 계산하여 반환하는 메소드를 작성하세요. public double getTotalSalary(List<Employee> employees) { return employees.stream() .mapToDouble(Employee::getSalary) .sum(); } // 특정 급여 이상의 직원 필터링 - 특정 급여(salary) 이상을 받는 직원들을 필터링하여 반환하는 메소드를 작성하세요. public List<Employee> filterBySalary(List<Employee> employees, double salary) { return employees.stream() .filter(employee -> employee.getSalary() >= salary) .collect(Collectors.toList()); } // // 부서별 평균 급여 계산 - 각 부서별로 평균 급여를 계산하여 맵 형태로 반환하는 메소드를 작성하세요. 부서 이름을 키로 하고 평균 // 급여를 값으로 하는 맵을 반환하세요. public Map<String, Double> getAverageSalaryByDepartment(List<Employee> employees) { return employees.stream() .collect(Collectors.groupingBy( Employee :: getDepartment, // e -> e.getDepartment() Collectors.averagingDouble(Employee :: getSalary))); // e -> e.getSalary() } // // 직원 이름 정렬 - 직원들의 이름을 알파벳 순서로 정렬하여 반환하는 메소드를 작성하세요. public List<Employee> sortEmployeesByName(List<Employee> employees) { return employees.stream() .sorted((emp1, emp2) -> emp1.getName().compareTo(emp2.getName())) .toList(); } // // 직원 ID로 직원 찾기 - 직원 ID로 특정 직원을 찾아 반환하는 메소드를 작성하세요. 직원이 존재하지 않으면 // Optional.empty()를 반환하세요. public Optional<Employee> findEmployeeById(List<Employee> employees, int id) { return employees.stream() .filter(employee -> employee.getId() == id) .findFirst(); } }
Java
복사
EmployeeExample.java
package com.assignment; import java.util.Arrays; import java.util.List; public class EmployeeExample { public static void main(String[] args) { List<Employee> employees = Arrays.asList( // 심유경, 유선웅, 설경하, 성인옥, 허형민 new Employee(1, "심유경", "Engineering", 75000, "sim@example.com"), new Employee(2, "유선웅", "Marketing", 60000, "ysw@example.com"), new Employee(3, "설경하", "Engineering", 80000, "snow@example.com"), new Employee(4, "성인옥", "Sales", 55000, "inok@example.com"), new Employee(5, "허형민", "Marketing", 70000, "hhhhhm@example.com") ); EMSystem system = new EMSystem(); System.out.println("부서별 필터링 (Engineering):"); system.filterByDepartment(employees, "Engineering").forEach(System.out::println); System.out.println("\n이메일 주소:"); system.getEmailAddresses(employees).forEach(System.out::println); System.out.println("\n총 급여:"); System.out.println(system.getTotalSalary(employees)); System.out.println("\n급여 필터링 (>= 70000):"); system.filterBySalary(employees, 70000).forEach(System.out::println); System.out.println("\n부서별 평균 급여:"); system.getAverageSalaryByDepartment(employees).forEach((dept, avgSalary) -> System.out.println(dept + ": " + avgSalary) ); System.out.println("\n직원명 정렬:"); system.sortEmployeesByName(employees).forEach(System.out::println); System.out.println("\nID로 찾기:"); system.findEmployeeById(employees, 3).ifPresent(System.out::println); } }
Java
복사