问题描述:
需要将一个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"
}
}
代码如下:文章来源:https://www.toymoban.com/news/detail-520044.html
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如下:文章来源地址https://www.toymoban.com/news/detail-520044.html
{
"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"
}
}
到了这里,关于java将map中的值复制到另一个map中的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!