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.
This part of code:
e -> e.stream().sorted().limit(limit).collect(Collectors.toList())
return List of List
and not List
, so either you change the type of the result to:
Map> groupByTeachers =
// ...The rest of your code
Or if you expect Map
, change the collectingAndThen
to get the expected field, for example getName
or getDep
:
e -> e.stream().sorted().limit(limit)
.map(Employee::getDep) // for example getDep
.collect(Collectors.toList())