I have a list of employees as follows:
[Employee{id="1", NID="A123", wages=5000},
Employee{id="2", NID="B123", wages=1000},
Employee{id="3", NID="C123", wages=2000},
Employee{id="4", NID="C123", wages=3000}]
I need to be able to retrieve only the unique objects and where NID is the same, I need to retrieve the one with the max Id. So the final list should be like
[Employee{id="1", NID="A123", wages=5000},
Employee{id="2", NID="B123", wages=1000},
Employee{id="4", NID="C123", wages=3000}]
I know it is possible to do it with nested for loops but I want to do it with Java streams. I could find which employee are duplicate in the list by using the group by but I still can't figure how to get the list like above.
Map<String, List<Employee>> groupByNid = employeeList.stream().collect(Collectors.groupingBy(Employee::getNID));
Thanks to help.
Ashley