Java 8,Lambda:在分组列表中排序并将所有组合并到列表中 [英] Java 8, Lambda: Sorting within grouped Lists and merging all groups to a list

查看:1486
本文介绍了Java 8,Lambda:在分组列表中排序并将所有组合并到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于以下答案: https://stackoverflow.com/a/30202075/8760211

如何按stud_id对每个组进行排序,然后通过stud_location返回一个包含所有学生的List作为分组结果,然后按stud_id排序)?

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)?

将它作为现有Lambda Expression的扩展名会很棒:

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"

结果将如下所示:

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"

我试过以下内容:

studlistGrouped.entrySet().stream().sorted(Comparator.compar‌​ing(Map.Entry::getVa‌​lue))

但是这不起作用。

推荐答案

如果我找对你,你想要一个列表< Student> (不是地图)学生按其位置分组,并按照组内的ID排序,其中组也按ID排序,而不是位置名称。这是可能的,但需要一次分组和两次排序:

If I get you right, you want a List<Student> (not a map) where students are grouped by their locations and sorted by ids inside groups and where groups are also sorted by ids, not by location names. This is possible, but requires one grouping and two sortings:

//first, use your function to group students
Map<String, List<Student>> studlistGrouped = students.stream()
        .collect(Collectors.groupingBy(Student::getLocation, Collectors.toList()));

//then sort groups by minimum id in each of them
List<Student> sorted = studlistGrouped.entrySet().stream()
        .sorted(Comparator.comparing(e -> e.getValue().stream().map(Student::getId).min(Comparator.naturalOrder()).orElse(0)))
        //and also sort each group before collecting them in one list
        .flatMap(e -> e.getValue().stream().sorted(Comparator.comparing(Student::getId))).collect(Collectors.toList());

这将产生以下结果:

Student{id='1726', name='John', location='New York'}
Student{id='3442', name='Mark', location='New York'}
Student{id='5223', name='Michael', location='New York'}
Student{id='2234', name='Andrew', location='Los Angeles'}
Student{id='4321', name='Max', location='California'}
Student{id='7765', name='Sam', location='California'}

也许这可以更优雅地完成,欢迎提出建议

Maybe this can be done more elegantly, suggestions are welcome

编辑:在撰写本答案时,没有提及基于OP中原始列表中元素顺序的分组题。所以我的假设是按ID分类列表和组。对于基于原始列表中的顺序的解决方案,请参阅其他答案,例如,Holgers one

At the time this answer was written there was no mention about Grouping based on the order of the Elements in the origin List in the OPs question. So my assumption was to sort both list and groups by ids. For solutions based on the order in the original list see other answers, for example, the Holgers one

这篇关于Java 8,Lambda:在分组列表中排序并将所有组合并到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆