博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

两个Map的合并操作

Posted on 2022-10-22 16:06  和风细雨汪汪  阅读(782)  评论(0编辑  收藏  举报

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

方式1:使用map的merge()方法进行合并
public class MergeTwoMaps {
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);

    //进行map的合并操作
    //方式1:使用map的merge()方法进行合并
    HashMap<Integer, Integer> map = new HashMap<>(map1);
    /*
    * merge(param1,param2,param3) : 第一个参数为要合并的key,第二个参数为要合并的value,第三个参数接收两个参数的函数,用来处理重复的
    * key值出现的处理逻辑,(v1,v2) -> v1)表示使用map1的value值,(v1,v2) -> v2)表示使用map2的value值
    * */
    map2.forEach((key,value) -> map.merge(key,value,(v1,v2) -> v1));
    map.forEach((key,value) -> System.out.println(key + ": " + value));
}

}

输出结果:

1: 1
2: 2
3: 3
4: 4
5: 5
6: 6
1
2
3
4
5
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
1
2
3
4
5
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
1
2
3
4
5
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
1
2
3
4
5
6