Java Map Merge deepMerge(Map m1, Map m2)

Here you can find the source of deepMerge(Map m1, Map m2)

Description

Perform a deep merge of two maps.

License

Apache License

Parameter

Parameter Description
K the type of the keys of the maps to merge
V the type of the values of the maps to merge
m1 the map to merge into
m2 the map to merge

Declaration

@SuppressWarnings("unchecked")
public static <K, V> void deepMerge(Map<K, V> m1,
        Map<? extends K, ? extends V> m2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Collection;
import java.util.Map;

public class Main {
    /**//from w w  w  . j  a  v a2 s.co  m
     * Perform a deep merge of two maps. Recursively merge the second map into
     * the first one. Merge nested maps and lists. Overwrite existing keys.
     * @param <K> the type of the keys of the maps to merge
     * @param <V> the type of the values of the maps to merge
     * @param m1 the map to merge into
     * @param m2 the map to merge
     */
    @SuppressWarnings("unchecked")
    public static <K, V> void deepMerge(Map<K, V> m1,
            Map<? extends K, ? extends V> m2) {
        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)) {
                    // Don't overwrite existing values if the collection already contains
                    // all values from the other collection. This is useful if we are
                    // trying to merge unmodifiable collections.
                } 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)) {
                    // Don't overwrite an existing value that is equal to the new value.
                    // This is useful if we are trying to merge unmodifiable maps.
                } else {
                    m1.put(k, v2);
                }
            }
        }
    }
}

Related

  1. deepMerge(Map dst, Map src)
  2. deepMerge(Map original, Map newMap)
  3. merge(final Map lhs, final Map rhs)
  4. merge(final Map map1, final Map map2)
  5. merge(final Map... maps)