Map的合并操作

本文介绍了Java中四种不同的Map合并方法:使用merge()方法、Stream.concat、Stream.of以及直接使用Stream的Collector。每个方法通过示例代码展示了如何合并两个Map,并在遇到相同键时处理冲突。这些方法适用于需要合并多个Map的场景。

两个Map的合并操作

两个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

方式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
### 如何合并 Sourcemap 文件 Sourcemap 文件对于前端开发中的调试至关重要,尤其是在经过多层编译和压缩之后。然而,在某些情况下,项目可能经历多个构建阶段,每个阶段都会生成自己的 sourcemap 文件。为了保持最终输出的一致性和可维护性,有时需要将这些 sourcemaps 合并。 #### 使用 `source-map` npm 包来处理 Sourcemap合并 可以利用社区中成熟的库来进行此操作,比如 `source-map` 这个 npm 包[^5]。该库提供了 API 来解析、修改以及合成新的 sourcemap 数据。下面是一段简单的 JavaScript 代码示例展示如何通过编程方式实现两个 sourcemap 文件的合并: ```javascript const { SourceMapConsumer, SourceMapGenerator } = require('source-map'); async function mergeSourceMaps(mapAPath, mapBPath) { const consumerA = await new Promise((resolve, reject) => SourceMapConsumer.with(mapAPath, null, resolve, reject)); const consumerB = await new Promise((resolve, reject) => SourceMapConsumer.with(mapBPath, null, resolve, reject)); let generator = new SourceMapGenerator({ file: 'output.min.js', sourceRoot: '' }); // Add mappings from both consumers to the generator. consumerA.eachMapping(mapping => generator.addMapping(mapping)); consumerB.eachMapping(mapping => generator.addMapping(mapping)); console.log(JSON.stringify(generator.toJSON(), null, 2)); } ``` 这段脚本展示了怎样加载现有的 sourcemap 并创建一个新的包含所有原始映射的新实例。最后打印出 JSON 格式的字符串表示形式以便进一步保存或应用到其他地方。 需要注意的是实际应用场景下可能会更复杂一些,因为不同版本之间可能存在冲突或者重复定义等问题。因此建议仔细阅读官方文档并根据具体需求调整逻辑[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值