合并两个Map< String,Integer>使用Java 8 Stream API [英] Merging two Map<String, Integer> with Java 8 Stream API

查看:644
本文介绍了合并两个Map< String,Integer>使用Java 8 Stream API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个(或更多) Map< String,Integer> 对象。我想将它们与Java 8 Stream API合并,使公共键的值应该是值的最大值。

  @Test 
public void test14()抛出异常{
Map< String,Integer> m1 = ImmutableMap.of(a,2,b,3);
Map< String,Integer> m2 = ImmutableMap.of(a,3,c,4);
List< Map< String,Integer>> list = newArrayList(m1,m2);

Map< String,Integer> mx = list.stream()... // TODO

Map< String,Integer> expected = ImmutableMap.of(a,3,b,3,c,4);
assertEquals(预期,mx);
}

如何将此测试方法设为绿色?



我玩收取收藏家一段时间没有任何成功。



ImmutableMap newArrayList 是从谷歌番石榴。)

解决方案

  @Test 
公共无效test14()抛出异常{
Map< String,Integer> m1 = ImmutableMap.of(a,2,b,3);
Map< String,Integer> m2 = ImmutableMap.of(a,3,c,4);

Map< String,Integer> mx = Stream.of(m1,m2)
.map(Map :: entrySet)//将每个地图转换为一个条目集
.flatMap(Collection :: stream)//将每个集合转换为进入流,然后
// 会连接 到位原设定
.collect的(
Collectors.toMap(//收集到地图
Map.Entry的:: getKey,//其中每个条目都基于
Map.Entry :: getValue,//对流中的条目
Integer :: max //这样如果
已存在一个值//一个给定的密钥,旧的
//的最大值和新值为


;

/ *如果要使用并行流创建地图,请使用以下内容
Map< String,Integer> mx = Stream.of(m1,m2)
.parallel()
.map(Map :: entrySet)//将每个地图转换为条目集
.flatMap(Collection :: stream )//将每个设定成一个条目流,则
// 符连接 它,以代替原始集合
.collect的(
Collectors.toConcurrentMap(//收集到地图
的Map.Entry ::信息getKey,//其中每个条目是根据
的Map.Entry ::的getValue,//在流
整数::最大的条目//这样,如果价值已经存在于
//给定的密钥,旧的
//的最大值和新值为


;
* /

Map< String,Integer> expected = ImmutableMap.of(a,3,b,3,c,4);
assertEquals(预期,mx);
}


I have two (or more) Map<String, Integer> objects. I'd like to merge them with Java 8 Stream API in a way that values for common keys should be the maximum of the values.

@Test
public void test14() throws Exception {
    Map<String, Integer> m1 = ImmutableMap.of("a", 2, "b", 3);
    Map<String, Integer> m2 = ImmutableMap.of("a", 3, "c", 4);
    List<Map<String, Integer>> list = newArrayList(m1, m2);

    Map<String, Integer> mx = list.stream()... // TODO

    Map<String, Integer> expected = ImmutableMap.of("a", 3, "b", 3, "c", 4);
    assertEquals(expected, mx);
}

How can I make this test method green?

I've played with collect and Collectors for a while without any success.

(ImmutableMap and newArrayList are from Google Guava.)

解决方案

@Test
public void test14() throws Exception {
    Map<String, Integer> m1 = ImmutableMap.of("a", 2, "b", 3);
    Map<String, Integer> m2 = ImmutableMap.of("a", 3, "c", 4);

    Map<String, Integer> mx = Stream.of(m1, m2)
        .map(Map::entrySet)          // converts each map into an entry set
        .flatMap(Collection::stream) // converts each set into an entry stream, then
                                     // "concatenates" it in place of the original set
        .collect(
            Collectors.toMap(        // collects into a map
                Map.Entry::getKey,   // where each entry is based
                Map.Entry::getValue, // on the entries in the stream
                Integer::max         // such that if a value already exist for
                                     // a given key, the max of the old
                                     // and new value is taken
            )
        )
    ;

    /* Use the following if you want to create the map with parallel streams
    Map<String, Integer> mx = Stream.of(m1, m2)
        .parallel()
        .map(Map::entrySet)          // converts each map into an entry set
        .flatMap(Collection::stream) // converts each set into an entry stream, then
                                     // "concatenates" it in place of the original set
        .collect(
            Collectors.toConcurrentMap(        // collects into a map
                Map.Entry::getKey,   // where each entry is based
                Map.Entry::getValue, // on the entries in the stream
                Integer::max         // such that if a value already exist for
                                     // a given key, the max of the old
                                     // and new value is taken
            )
        )
    ;
    */

    Map<String, Integer> expected = ImmutableMap.of("a", 3, "b", 3, "c", 4);
    assertEquals(expected, mx);
}

这篇关于合并两个Map&lt; String,Integer&gt;使用Java 8 Stream API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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