Java 8 流:迭代列表映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27448266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:23:30  来源:igfitidea点击:

Java 8 streams: iterate over Map of Lists

javahashmapjava-8java-stream

提问by waXve

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.

我想在另一个地图中转换地图。结果图的Key就是输入图的key。结果映射的值是 My 对象的属性“名称”,按优先级排序。

The orderingand 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.

排序和提取的名字是没有问题的,但我不能把它放到结果地图。我用旧的 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);
}

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

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

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

采纳答案by mkobit

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::getNameas the mapping and collected the resulting names into a list.

从本质上讲,您可以流式传输每个条目集并将其收集到一个新地图中。要计算新地图中的值,您需要List<MyOjbect>对旧地图中的 进行流式处理,对其进行排序,然后对其应用映射和收集函数。在这种情况下,我用作MyObject::getName映射并将结果名称收集到一个列表中。

回答by nitishagar

For generating another map, we can have something like following:

为了生成另一个地图,我们可以有如下内容:

HashMap<String, List<String>> result = anotherHashMap.entrySet().stream().collect(Collectors.toMap(elem -> elem.getKey(), elem -> elem.getValue() // can further process it);

Above I am recreating the map again, but you can process the key or the value according to your needs.

上面我再次重新创建地图,但您可以根据需要处理键或值。

回答by Alex Filatov

Map<String, List<String>> result = anotherHashMap.entrySet().stream().collect(Collectors.toMap(
    Map.Entry::getKey,
    e -> e.getValue().stream()
        .sorted(comparing(MyObject::getPriority))
        .map(MyObject::getName)
        .collect(Collectors.toList())));

Similar to answer of Mike Kobit, but sorting is applied in the correct place (i.e. value is sorted, not map entries) and more concise static method Comparator.comparingis used to get Comparator for sorting.

类似于 Mike Kobit 的回答,但排序应用在正确的位置(即排序值,而不是映射条目)并且使用更简洁的静态方法Comparator.comparing来获取 Comparator 进行排序。