Find N’th Highest Salary Employee in Each Department
- Group employees by department
- Collectors.groupingBy
- For each department:
-
Sort employees by salary in descending order
- sorted(Comparator.comparing(Employee::getSalary).reversed())
- Skip the highest salary employee skip
- Pick the next one (2nd highest) findFirst
-
- Store result as:
Map<Department, Optional<Employee>>ORMap<Department, Employee>(if data is guaranteed)
2nd Highest Salary Employee in Each Department
public static Map<String, Optional<Employee>> getSecondHighestSalaryEmployeeByDepartment(List<Employee> employees) {
return employees.stream().collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.collectingAndThen(Collectors.toList(),list -> list.stream().
sorted(Comparator.comparing(Employee::getSalary).reversed())
.skip(1)
.findFirst())
));
}
First Highest Salary Employee in Each Department
public static Map<String, Optional<Employee>> getFirstHighestSalaryEmployeeByDepartment(List<Employee> employees) {
return employees.stream().collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.collectingAndThen(Collectors.toList(),list ->
list.stream().max(Comparator.comparing(Employee::getSalary)))
));
}
Least Salary Employee in Each Department
public static Map<String, Optional<Employee>> getLeastSalaryEmployeeByDepartment(List<Employee> employees) {
return employees.stream().collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.collectingAndThen(Collectors.toList(),list ->
list.stream().min(Comparator.comparing(Employee::getSalary)))
));
}