class Employee {
String name, department;
double salary;
Employee(String name, String department, double salary) {
this.name = name;
this.department = department;
this.salary = salary;
}
}
List
new Employee("Alice", "HR", 50000),
new Employee("Bob", "IT", 80000),
new Employee("Charlie", "IT", 75000),
new Employee("Dave", "HR", 60000),
new Employee("Eve", "Finance", 70000)
);
Map
.collect(Collectors.groupingBy(
e -> e.department,
Collectors.collectingAndThen(
Collectors.maxBy(Comparator.comparingDouble(e -> e.salary)),
Optional::get
)
));
System.out.println(highestPaid);
// Output: {HR=Dave, IT=Bob, Finance=Eve}
Log in to add a comment.