Java 8 lambda for selecting top salary employee for each department
You can do that with a grouping collector: Map<String, Employee> topEmployees = allEmployees.stream() .collect(groupingBy( e -> e.department, collectingAndThen(maxBy(comparingInt(e -> e.salary)), Optional::get) )); with the static imports import static java.util.Comparator.comparingInt; import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.maxBy; This code creates a Stream of all the employees and groups them with their department with the … Read more