I have the following list of Employee data which I need to group based on the employee department and then I want to find the 2 highest-paid employees in each department.
Here is the final answer for those who are interested.
Map> groupByTeachers =
listOfEmp.stream()
.collect(
Collectors.groupingBy(
Employee::getDept,
Collectors.collectingAndThen(
Collectors.toList(),
e -> e.stream().sorted(Comparator.comparingLong(Employee::getSalary).reversed()).limit(limit).map(Employee::getName).collect(toList() ) ) ) );