You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java如何找出HashMap需增删的键值对以匹配目标HashMap内容

解决方案

实现思路

  • 遍历两个Map的全部键的并集,按不同场景计算差值:
    • 仅在目标Map(dictOP)中存在的键:整组值归入新增集合
    • 仅在原Map(dictMobile)中存在的键:整组值归入删除集合
    • 两个Map都存在的键:分别计算两个值列表的差集,目标有原Map没有的归入新增,原Map有目标没有的归入删除,差集为空的不加入结果

完整代码实现

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Map<String, List<String>> dictOP = new HashMap<>();
        Map<String, List<String>> dictMobile = new HashMap<>();

        // 初始化数据集,和你提供的一致
        List<String> obj1Mobile = new ArrayList<>();
        List<String> obj2Mobile = new ArrayList<>();
        List<String> obj3Mobile = new ArrayList<>();
        List<String> obj1Op = new ArrayList<>();
        List<String> obj2Op = new ArrayList<>();
        List<String> obj3Op = new ArrayList<>();

        obj1Mobile.add("Argentina");
        obj1Mobile.add("Chile");
        obj1Mobile.add("Brazil");
        obj2Mobile.add("Brazil");
        obj3Mobile.add("Russia");

        obj1Op.add("Argentina");
        obj1Op.add("Brazil");
        obj2Op.add("Brazil");
        obj2Op.add("China");
        obj3Op.add("Germany");

        dictOP.put("obj1", obj1Op);
        dictOP.put("obj2", obj2Op);
        dictOP.put("obj3Op", obj3Op);
        dictMobile.put("obj1", obj1Mobile);
        dictMobile.put("obj2", obj2Mobile);
        dictMobile.put("obj3Mobile", obj3Mobile);

        // 计算差异的核心逻辑
        Map<String, List<String>> addMap = new HashMap<>();
        Map<String, List<String>> removeMap = new HashMap<>();

        // 获取两个Map所有键的并集
        Set<String> allKeys = new HashSet<>();
        allKeys.addAll(dictMobile.keySet());
        allKeys.addAll(dictOP.keySet());

        for (String key : allKeys) {
            List<String> mobileList = dictMobile.get(key);
            List<String> opList = dictOP.get(key);

            // 情况1:仅目标map存在该键,全部新增
            if (mobileList == null) {
                addMap.put(key, new ArrayList<>(opList));
                continue;
            }
            // 情况2:仅原map存在该键,全部删除
            if (opList == null) {
                removeMap.put(key, new ArrayList<>(mobileList));
                continue;
            }
            // 情况3:两个都有,计算差值
            // 计算要新增的元素:op有,mobile没有
            List<String> toAdd = new ArrayList<>(opList);
            toAdd.removeAll(mobileList);
            if (!toAdd.isEmpty()) {
                addMap.put(key, toAdd);
            }
            // 计算要删除的元素:mobile有,op没有
            List<String> toRemove = new ArrayList<>(mobileList);
            toRemove.removeAll(opList);
            if (!toRemove.isEmpty()) {
                removeMap.put(key, toRemove);
            }
        }

        // 输出结果
        System.out.println("add:  " + addMap);
        System.out.println("remove: " + removeMap);
    }
}

运行结果

add:  {obj2=[China], obj3Op=[Germany]}
remove: {obj3Mobile=[Russia], obj1=[Chile]}

完全符合预期输出要求。

内容的提问来源于stack exchange,提问作者William

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 20:15:02