两个Map的合并操作

两个map进行合并有多种方式实现,以下列举出几种常见的合并方式:

方式1:使用map的merge()方法进行合并

merge()  函数的作用是: 如果给定的key之前没设置value 或者value为null, 则将给定的value关联到这个key上.否则,通过给定的remaping函数计算的结果来替换其value。如果remapping函数的计算结果为null,将解除此结果。

就是,把一个map循环合并到另一个map 例如:

map.forEach((k, v) -> map2.merge(k, v, (v1, v2) -> v2));

map2的结果是map2和map的lambda结果集,而map则还是原来的map

 merge(param1,param2,param3) : 第一个参数为要合并的key,第二个参数为要合并的value,第三个参数接收两个参数的函数,用来处理重复的 key值出现的处理逻辑,(v1,v2) -> v1)表示使用map1的value值,(v1,v2) -> v2)表示使用map2的value值 

public static void main(String[] args) {
        HashMap<String, String> specialPrice = new HashMap<>();
        specialPrice.put("1", "1");
        specialPrice.put("2", "2");
        specialPrice.put("3", "3");
        HashMap<String, String> collect = new HashMap<>();
        collect.put("4", "4");
        collect.put("3", "5");
        collect.put("6", "6");
        collect.forEach((k, v) -> specialPrice.merge(k, v, (v1, v2) -> v2));

        for (Map.Entry<String, String> stringStringEntry : specialPrice.entrySet()) {
            System.out.println(stringStringEntry.getKey() + "===>" + stringStringEntry.getValue());
        }
    }

输出结果

1===>1
2===>2
3===>5
4===>4
6===>6

方式2: 使用Stream.concat进行合并

public class MergeTwoMaps2 {
    public static void main(String[] args) {
        Map<Integer,Integer> map1 = new HashMap<>();
        map1.put(1,1);
        map1.put(2,2);
        map1.put(3,3);
        map1.put(4,4);

        Map<Integer,Integer> map2 = new HashMap<>();
        map2.put(1,0);
        map2.put(5,5);
        map2.put(6,6);

        //将map1和map2收集成一个流
        Stream<Map.Entry<Integer, Integer>> concat = Stream.concat(map1.entrySet().stream(), map2.entrySet().stream());
        //然后将其收集成一个新的map
        Map<Integer, Integer> map = concat.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v2));
        map.forEach((key,value) -> {
            System.out.println(key + ": " + value);
        });
    }
}

输出结果:

1: 0
2: 2
3: 3
4: 4
5: 5
6: 6

方式3:使用Stream.of进行合并

public class MergeTwoMaps3 {
    public static void main(String[] args) {
        Map<Integer,Integer> map1 = new HashMap<>();
        map1.put(1,1);
        map1.put(2,2);
        map1.put(3,3);
        map1.put(4,4);

        Map<Integer,Integer> map2 = new HashMap<>();
        map2.put(1,0);
        map2.put(5,5);
        map2.put(6,6);

        //注意: 和contact不同的是stream.of可以初始化多个元素,然后用扁平化的处理成需要的流,然后用收集器来转为Map
        Stream<Map<Integer, Integer>> stream = Stream.of(map1, map2);
        Map<Integer, Integer> map = stream.flatMap(m -> m.entrySet().stream())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
        map.forEach((key,value) -> {
            System.out.println(key + ": " + value);
        });
    }
}

输出结果:

1: 1
2: 2
3: 3
4: 4
5: 5
6: 6

方式4:直接使用Steam的Collector进行收集合并

public class MergeTwoMaps4 {
    public static void main(String[] args) {
        Map<Integer,Integer> map1 = new HashMap<>();
        map1.put(1,1);
        map1.put(2,2);
        map1.put(3,3);
        map1.put(4,4);

        Map<Integer,Integer> map2 = new HashMap<>();
        map2.put(1,0);
        map2.put(5,5);
        map2.put(6,6);

        //方式4:直接使用Collector进行收集
        HashMap<Integer, Integer> map = map2.entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v2, () -> new HashMap<>(map1)));
        map.forEach((key,value) -> {
            System.out.println(key + ": " + value);
        });
    }
}

输出结果:

1: 0
2: 2
3: 3
4: 4
5: 5
6: 6

  • 9
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值