相关疑难解决方法(0)

在流上使用Collections.toMap()时,如何保持List的迭代顺序?

我创建一个MapList如下:

List<String> strings = Arrays.asList("a", "bb", "ccc");

Map<String, Integer> map = strings.stream()
    .collect(Collectors.toMap(Function.identity(), String::length));
Run Code Online (Sandbox Code Playgroud)

我希望保持与之相同的迭代顺序List.如何创建LinkedHashMap使用Collectors.toMap()方法?

java collections java-8 java-stream

70
推荐指数
2
解决办法
4万
查看次数

如何从方法Collectors.groupingBy获取有序类型的Map

我需要按类型将数据列表分成不同的列表,为此我使用构造

Map<String,List<Dish>> dishMap = menu.stream()  
         .collect(Collectors.groupingBy(Dish::getType));
Run Code Online (Sandbox Code Playgroud)

但是如何从方法"Collectors.groupingBy"获取LinkedHashMap而不是HashMap.我在javadoc中找到了一些数据但是我不能得到我必须用这个方法做的事情:

Map<String,List<Dish>> dishMap = menu.stream().collect(Collectors.groupingBy(
                Dish::getType,
                LinkedHashMap::new, ????));
Run Code Online (Sandbox Code Playgroud)

我应该在方法"groupingBy"的第三个参数中放置什么才能得到我需要的东西?

java grouping linkedhashmap java-8 collectors

10
推荐指数
1
解决办法
4546
查看次数