Based on the following answer: https://stackoverflow.com/a/30202075/8760211
How to sort each group by stud_id and then return a List with all Students as result of the grouping by stud_location and then sorting by stud_id)?
It would be great to have this as extension to the existing Lambda Expression:
Map<String, List<Student>> studlistGrouped =
studlist.stream().collect(Collectors.groupingBy(w -> w.stud_location));
I need the Grouping based on the order of the Elements in the origin List.
First group: "New York"
Second group: "California"
Third group: "Los Angeles"
1726, "John", "New York"
4321, "Max", "California"
2234, "Andrew", "Los Angeles"
5223, "Michael", "New York"
7765, "Sam", "California"
3442, "Mark", "New York"
The result would then look like the following:
List<Student> groupedAndSorted = ....
1726, "John", "New York"
3442, "Mark", "New York"
5223, "Michael", "New York"
4321, "Max", "California"
7765, "Sam", "California"
2234, "Andrew", "Los Angeles"
I have tried the following:
studlistGrouped.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue))
But this doesn't work.