java将map中的值复制到另一个map中

问题描述:
需要将一个sourceMap中设置的属性值copy到另一个targetMap中去,要求在sourceMap中没有设置的值,targetMap要保持不变,并且支持List中有Map的情况的复制。
**解决方案:**递归复制map中的值,List中如果是map的话则继续递归复制值。代码在最后面
sourceMap如下:

{
  "list2": [1,2,3,4],
  "list1": [
    {
      "key1": "value1111"
    },
    {
      "key2": "value22222"
    }
  ],
  "attribute": {
    "aliasname": "Read DB",
    "name": "READ DBxxxx",
    "activated": "truexxxxxxx",
    "compatibility": "5.3.015xxxxxxxx"
  }
}

targetMap如下:

{
  "list2": [1,2],
  "list1": [
    {
      "key1": "value1",
      "key11": "value11"
    },
    {
      "key2": "value2"
    }
  ],
  "attribute": {
    "aliasname": "Read DB",
    "name": "READ DB",
    "activated": "true",
    "compatibility": "5.3.015",
    "expanded": "true",
    "height": "28",
    "width": "140",
    "x": "366",
    "y": "96",
    "class": "load_db"
  }
}

代码如下:

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class MapUtils {

    /**
     * 将sourceMap中的值复制到targetMap中去
     *
     * @param sourceMap
     * @param targetMap
     */
public static void copyValue(Map<String, Object> sourceMap, Map<String, Object> targetMap) {
        for (Map.Entry<String, Object> entry : sourceMap.entrySet()) {
            if (entry.getValue() instanceof List) {
                // 如果List中是map的话,按顺序copy对应的map里面的value
                List<Object> sourceList = (List) entry.getValue();
                List<Object> targetList = (List) targetMap.get(entry.getKey());
                if (!CollectionUtils.isEmpty(sourceList)) {
                    int size = Math.min(sourceList.size(), targetList.size());
                    for (int i = 0; i < size; i++) {
                        Object source = sourceList.get(i);
                        Object target = targetList.get(i);
                        if (source instanceof Map) {
                            copyValue((Map<String, Object>) source, (Map<String, Object>) target);
                        }
                        else {
                            targetMap.put(entry.getKey(), entry.getValue());
                        }
                    }
                }
                else {
                    targetMap.put(entry.getKey(), entry.getValue());
                }
            }
            else if (entry.getValue() instanceof Map) {
                // 递归copy值
                copyValue((Map<String, Object>) entry.getValue(), (Map<String, Object>) targetMap.get(entry.getKey()));
            }
            else {
                targetMap.put(entry.getKey(), entry.getValue());
            }
        }
    }

    /**
     * 解析json文件生成参数
     *
     * @param paramFilePath
     * @return
     * @throws IOException
     */
    public static Map<String, Object> parseParam(String paramFilePath) throws IOException {
        FileReader reader = new FileReader(paramFilePath);
        StringBuffer stringBuffer = new StringBuffer();
        char[] buff = new char[1024];
        int len = 0;
        while ((len = reader.read(buff)) != -1) {
            stringBuffer.append(new String(buff, 0, len));
        }
        String param = stringBuffer.toString();
        Map<String, Object> paramMap = JSONObject.parseObject(param);
        reader.close();
        return paramMap;
    }

    public static void main(String[] args) throws IOException {
        Long start = System.currentTimeMillis();
        String paramFilePath = "./data/json_data/targetParam.json";
        String sourceParamPath = "./data/json_data/sourceParam.json";
        Map<String, Object> targetParam = parseParam(paramFilePath);
        Map<String, Object> sourceParam = parseParam(sourceParamPath);
        copyValue(sourceParam, targetParam);
        System.out.println(targetParam);
        Long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}

处理后的targetMap如下:

{
  "list1": [
    {
      "key1": "value1111",
      "key11": "value11"
    },
    {
      "key2": "value22222"
    },
  ],
  "list2": [1,2,3,4],
  "attribute": {
    "aliasname": "Read DB",
    "expanded": "true",
    "name": "READ DBxxxx",
    "width": "140",
    "x": "366",
    "y": "96",
    "compatibility": "5.3.015xxxxxxxx",
    "class": "load_db",
    "activated": "truexxxxxxx",
    "height": "28"
  }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值