Java 8流:遍历列表映射 [英] Java 8 streams: iterate over Map of Lists

查看:295
本文介绍了Java 8流:遍历列表映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下对象和一个Map:

  MyObject 
字符串名称;
长期优先;
foo bar;

地图< String,List< MyObject>> anotherHashMap;

我想在另一个地图中转换地图。结果图的关键是输入图的关键。结果图的值是我的对象的属性名称,按优先级排序。



命令 并提取名称不是这个问题,但我不能把它放到结果图中。我使用旧的Java 7方式,但它可以很好地使用流API。

  Map< String ,List< String>> result = new HashMap<>(); 
for(String identifier:anotherHashMap.keySet()){
List< String> generatedList = anotherHashMap.get(identifier).stream()...;

teaserPerPage.put(identifier,generatedList);
}

有没有人有想法?我试过这个,但是被卡住了:

  anotherHashMap.entrySet()。stream()。collect(Collectors.asMap(..) ...,)); 


解决方案

  Map<字符串,列表< String>> result = anotherHashMap 
.entrySet()。stream()// Stream over entry set
.collect(Collectors.toMap(// Collect final result map
Map.Entry :: getKey,/ / Key键映射是相同的
e - > e.getValue()。stream()// Stream over list
.sorted(Comparator.comparingLong(MyObject :: getPriority))//按优先级排序
.map(MyObject :: getName)//将映射应用到MyObject
.collect(Collectors.toList()))//收集映射到列表
);基本上,您可以在每个条目集上进行流式处理,并将其收集到新的地图中。要计算新地图中的值,您需要通过旧地图上的 List< MyOjbect> 进行流式处理,对其进行排序,然后将映射和收集功能应用于该地图。在这种情况下,我使用 MyObject :: getName 作为映射,并将结果名称收集到列表中。


I have the following Object and a Map:

MyObject
    String name;
    Long priority;
    foo bar;

Map<String, List<MyObject>> anotherHashMap;

I want to convert the Map in another Map. The Key of the result map is the key of the input map. The value of the result map ist the Property "name" of My object, ordered by priority.

The ordering and extracting the name is not the problem, but I could not put it into the result map. I do it the old Java 7 way, but it would be nice it is possible to use the streaming API.

Map<String, List<String>> result = new HashMap<>();
for (String identifier : anotherHashMap.keySet()) {
    List<String> generatedList = anotherHashMap.get(identifier).stream()...;

    teaserPerPage.put(identifier, generatedList);
}

Has anyone an idea? I tried this, but got stuck:

anotherHashMap.entrySet().stream().collect(Collectors.asMap(..., ...));

解决方案

Map<String, List<String>> result = anotherHashMap
    .entrySet().stream()                    // Stream over entry set
    .collect(Collectors.toMap(              // Collect final result map
        Map.Entry::getKey,                  // Key mapping is the same
        e -> e.getValue().stream()          // Stream over list
            .sorted(Comparator.comparingLong(MyObject::getPriority)) // Sort by priority
            .map(MyObject::getName)         // Apply mapping to MyObject
            .collect(Collectors.toList()))  // Collect mapping into list
        );

Essentially, you stream over each entry set and collect it into a new map. To compute the value in the new map, you stream over the List<MyOjbect> from the old map, sort, and apply a mapping and collection function to it. In this case I used MyObject::getName as the mapping and collected the resulting names into a list.

这篇关于Java 8流:遍历列表映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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