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

后端 未结 9 1311
难免孤独
难免孤独 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 05:01

    I was facing same issue and I tried all the solutions but not able to fix the problem, Then I tried following way and finally I am able to fix my problem.

    In above example my studlist was already sorted but still its generating map in any order that was main question.

    Map> studlistGrouped =
        studlist.stream().collect(Collectors.groupingBy(w -> w.stud_location));
    **studlistGrouped = new TreeMap>(studlistGrouped);**
    

    So using this above solution it sorted all the keys in Map. Our goal to get result into Map only then why people try to converting it into list?

提交回复
热议问题