Java Utililty Methods Map Merge

List of utility methods to do Map Merge

Description

The list of methods to do Map Merge are organized into topic(s).

Method

MapdeepMerge(Map dst, Map src)
Deep merges two maps
if (dst != null && src != null) {
    for (Object key : src.keySet()) {
        if (src.get(key) instanceof Map && dst.get(key) instanceof Map) {
            Map originalChild = (Map) dst.get(key);
            Map newChild = (Map) src.get(key);
            dst.put(key, deepMerge(originalChild, newChild));
        } else {
            dst.put(key, src.get(key));
...
voiddeepMerge(Map m1, Map m2)
Perform a deep merge of two maps.
for (K k : m2.keySet()) {
    V v1 = m1.get(k);
    V v2 = m2.get(k);
    if (v1 instanceof Collection && v2 instanceof Collection) {
        if (((Collection<Object>) v1).containsAll((Collection<Object>) v2)) {
        } else {
            ((Collection<Object>) v1).addAll((Collection<Object>) v2);
    } else if (v1 instanceof Map && v2 instanceof Map) {
        deepMerge((Map<Object, Object>) v1, (Map<Object, Object>) v2);
    } else {
        V existingV = m1.get(k);
        if (existingV != null && existingV.equals(v2)) {
        } else {
            m1.put(k, v2);
MapdeepMerge(Map original, Map newMap)
deep Merge
for (String key : newMap.keySet()) {
    if (newMap.get(key) instanceof Map && original.get(key) instanceof Map) {
        Map<String, Object> originalChild = (Map<String, Object>) original.get(key);
        Map<String, Object> newChild = (Map) newMap.get(key);
        original.put(key, deepMerge(originalChild, newChild));
    } else {
        original.put(key, newMap.get(key));
return original;
Mapmerge(final Map lhs, final Map rhs)
Clones the lhs map and add all things from the rhs map.
Map<K, V> result = null;
if (lhs == null || lhs.size() == 0) {
    result = copy(rhs);
} else if (rhs == null || rhs.size() == 0) {
    result = copy(lhs);
} else {
    result = copy(lhs);
    result.putAll(rhs);
...
Mapmerge(final Map map1, final Map map2)
merge
final HashMap<K, V> ret = new HashMap<K, V>(map1);
ret.putAll(map2);
return ret;
HashMapmerge(final Map... maps)
Merges specified maps into one new map and returns it.
int size = 0;
for (final Map<K, V> map : maps) {
    if (map != null) {
        size += map.size();
final HashMap<K, V> merged = new HashMap<K, V>(size);
for (final Map<K, V> map : maps) {
...
Map>merge(final Map> targetContext, final Map> newContext)
Merge one context onto another by adding or replacing values.
final HashMap<String, Map<String, String>> result = deepCopy(targetContext);
for (final Map.Entry<String, Map<String, String>> entry : newContext.entrySet()) {
    if (!targetContext.containsKey(entry.getKey())) {
        result.put(entry.getKey(), new HashMap<String, String>());
    } else {
        result.put(entry.getKey(), new HashMap<String, String>(targetContext.get(entry.getKey())));
    result.get(entry.getKey()).putAll(entry.getValue());
...
Mapmerge(List> list)
Merge/combine a list of Map into a single map
if ((list == null) || (list.isEmpty())) {
    return null;
Map<Character, Long> merged = new HashMap<>();
for (Map<Character, Long> map : list) {
    if ((map == null) || (map.isEmpty())) {
        continue;
    for (Map.Entry<Character, Long> entry : map.entrySet()) {
        char base = entry.getKey();
        Long count = merged.get(base);
        if (count == null) {
            merged.put(base, entry.getValue());
        } else {
            merged.put(base, (count + entry.getValue()));
return merged;
HashMapmerge(Map map1, Map map2)
Creates a new HashMap that has all of the elements of map1 and map2 (on key collision, the latter override the former).
HashMap retval = new HashMap(calcCapacity(map1.size() + map2.size()));
retval.putAll(map1);
retval.putAll(map2);
return retval;
voidmerge(Map options, Map addition)
merge
for (Object o : addition.entrySet()) {
    Map.Entry entry = (Map.Entry) o;
    if (entry.getValue() == null)
        options.remove(entry.getKey());
    else
        options.put(entry.getKey(), entry.getValue());