Java 8, Lambda: Sorting within grouped Lists and merging all groups to a list

后端 未结 9 1304
难免孤独
难免孤独 2021-02-09 04:21

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 g

9条回答
  •  有刺的猬
    2021-02-09 04:39

    If you just want to group and sort, you don't need a groupingBy() collector, or even a stream at all. Just use a composite sort:

    studlist.sort(Comparator.comparing(Student::getLocation).thenComparing(Student::getId));
    

提交回复
热议问题