Loading...
0

class Employee {
String name, department;
double salary;
Employee(String name, String department, double salary) {
this.name = name;
this.department = department;
this.salary = salary;
}
}

List employees = Arrays.asList(
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 highestPaid = employees.stream()
.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}

asked 1 week ago
sunil 0

Log in to add a comment.

0 Answers

No answers yet. Log in to answer.
Log in to answer this question.
Question Stats
Asked: 1 week ago
Views: 41
Active: 8 hours ago
Related Tags