如何从方法Collectors.groupingBy获取有序的Map类型 [英] How to get ordered type of Map from method Collectors.groupingBy
本文介绍了如何从方法Collectors.groupingBy获取有序的Map类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要按类型将数据列表分为不同的列表,为此,我使用了构造方法
I need to separate list of data into different lists by type, for this purpose I use construction
Map<String,List<Dish>> dishMap = menu.stream()
.collect(Collectors.groupingBy(Dish::getType));
但是如何从方法"Collectors.groupingBy"中获取LinkedHashMap而不是HashMap.我在javadoc中发现了一些数据,但无法获得使用该方法必须做的事情:
but How can I get LinkedHashMap instead HashMap from method "Collectors.groupingBy". I found some data in javadoc but I can`t get what I must to do with this method:
Map<String,List<Dish>> dishMap = menu.stream().collect(Collectors.groupingBy(
Dish::getType,
LinkedHashMap::new, ????));
我应该在方法"groupingBy"的第三个参数中放置什么以获取所需的信息?
what should I place in the third argument in method "groupingBy" to get what I need?
推荐答案
您应使用 toList
Collector
:
Map<String,List<Dish>> dishMap = menu.stream().collect(Collectors.groupingBy(
Dish::getType,
LinkedHashMap::new, Collectors.toList()));
这篇关于如何从方法Collectors.groupingBy获取有序的Map类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文