@Test(expected = UnsupportedOperationException.class) public void mergeUnmodifiableMapsDifferent() { Map<String, String> m1 = new HashMap<>(); m1.put("a", "1"); m1.put("b", "0"); m1 = Collections.unmodifiableMap(m1); Map<String, String> m2 = new HashMap<>(); m2.put("a", "1"); m2.put("b", "2"); m2 = Collections.unmodifiableMap(m2); MapUtils.deepMerge(m1, m2); }
@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)) { } 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); } } } }
MapUtils { @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)) { } 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); } } } } }
MapUtils { @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)) { } 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); } } } }  }
MapUtils { @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)) { } 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); } } } }  @SuppressWarnings("unchecked") static void deepMerge(Map<K, V> m1, Map<? extends K, ? extends V> m2); }
MapUtils { @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)) { } 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); } } } }  @SuppressWarnings("unchecked") static void deepMerge(Map<K, V> m1, Map<? extends K, ? extends V> m2);  }