mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-09 20:57:23 +08:00
feat(core): MapUtils增加深度合并两个map的方法 (#16)
This commit is contained in:
@@ -16,8 +16,9 @@
|
|||||||
|
|
||||||
package top.continew.starter.core.util;
|
package top.continew.starter.core.util;
|
||||||
|
|
||||||
import java.util.Map;
|
import cn.hutool.core.map.MapUtil;
|
||||||
import java.util.Properties;
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map 工具类
|
* Map 工具类
|
||||||
@@ -41,4 +42,50 @@ public class MapUtils {
|
|||||||
properties.putAll(source);
|
properties.putAll(source);
|
||||||
return properties;
|
return properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 深度合并两个map
|
||||||
|
* 需要用新的map接收
|
||||||
|
*
|
||||||
|
* @param to 需要合并的map
|
||||||
|
* @param from 需要被合并的map
|
||||||
|
* @return Map<String, Object> 必须重新使用的map
|
||||||
|
* @author luoqiz
|
||||||
|
* @since 2.14.0
|
||||||
|
*/
|
||||||
|
public static Map<String, Object> mergeMap(Map<String, Object> to, Map<String, Object> from) {
|
||||||
|
if (MapUtil.isEmpty(to)) {
|
||||||
|
return from;
|
||||||
|
}
|
||||||
|
if (MapUtil.isEmpty(from)) {
|
||||||
|
return to;
|
||||||
|
}
|
||||||
|
if (MapUtil.isEmpty(to) && MapUtil.isEmpty(from)) {
|
||||||
|
return new HashMap<>();
|
||||||
|
}
|
||||||
|
Set<Map.Entry<String, Object>> entries = to.entrySet();
|
||||||
|
Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
Map.Entry<String, Object> kv = iterator.next();
|
||||||
|
String toKey = kv.getKey();
|
||||||
|
Object toValue = kv.getValue();
|
||||||
|
Object fromValue = from.get(toKey);
|
||||||
|
if (fromValue != null) {
|
||||||
|
if (toValue instanceof Map) {
|
||||||
|
Map<String, Object> childTo = (Map<String, Object>)toValue;
|
||||||
|
mergeMap(childTo, (Map<String, Object>)fromValue);
|
||||||
|
} else {
|
||||||
|
to.put(toKey, fromValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<String> keys = from.keySet();
|
||||||
|
for (String key : keys) {
|
||||||
|
if (!to.containsKey(key)) {
|
||||||
|
to.put(key, from.get(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return to;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user