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

Map>mergeMapIntoMap(Map> source, Map> destination)
merge Map Into Map
if (destination == null) {
    return source;
if (source == null) {
    return destination;
source.keySet().stream().forEach(k -> {
    if (destination.containsKey(k)) {
...
MapmergeMapList(List> list)
merge Map List
Map<K, V> ret = new HashMap<K, V>();
for (Map<K, V> listEntry : list) {
    if (listEntry == null) {
        continue;
    for (Entry<K, V> mapEntry : listEntry.entrySet()) {
        K key = mapEntry.getKey();
        V value = mapEntry.getValue();
...
MapmergeMaps(Map m1, Map m2)
same as clojure's (merge-with merge m1 m2)
if (m2 == null) {
    return m1;
for (Object o : m2.entrySet()) {
    Map.Entry entry = (Map.Entry) o;
    Object k = entry.getKey();
    Map existing = (Map) m1.get(k);
    if (existing == null) {
...
MapmergeMaps(Map mainMap, Map defaultMap, String[] keys, boolean enforceValues)
merge Maps
final String eLabel = "MapHelper.MergeMaps: ";
HashMap returnMap = new HashMap();
if (defaultMap == null) {
    defaultMap = new HashMap();
try {
    for (int i = 0; i < keys.length; i++) {
        if (!mainMap.containsKey(keys[i]) && !defaultMap.containsKey(keys[i])) {
...
voidmergeMaps(Map dest, Map inserts)
merge Maps
for (Map.Entry<K, V> entry : inserts.entrySet()) {
    K key = entry.getKey();
    V value = entry.getValue();
    if (!dest.containsKey(key) || (dest.containsKey(key) && dest.get(key) != value)) {
        dest.put(key, value);
voidmergeMaps(Map into, Map from, boolean override)
Merge maps.
for (K key : from.keySet()) {
    if (override || !into.containsKey(key)) {
        into.put(key, from.get(key));
MapmergeMaps(Map m1, Map m2)
merge Maps
Map<K, V> m = new HashMap<>();
if (m1 != null)
    m.putAll(m1);
if (m2 != null)
    m.putAll(m2);
return m;
MapmergeMaps(Map map1, Map map2)
Returns a new map with all the entries of map1 and any from map2 which don't override map1.
Map<K, V> answer = new HashMap<>();
if (map2 != null) {
    answer.putAll(map2);
if (map1 != null) {
    answer.putAll(map1);
return answer;
...
MapmergeMaps(Map defaultMap, Map customMap)
Recursively merges a custom map into a default map, and returns the merged result.
for (String key : defaultMap.keySet()) {
    if (!customMap.containsKey(key)) {
        continue;
    Object newValue;
    if (defaultMap.get(key) instanceof Map && !key.endsWith("Map")) {
        newValue = mergeMaps((Map<String, Object>) defaultMap.get(key),
                (Map<String, Object>) customMap.get(key));
...
voidmergeMaps(Map destination, Map source)

Merges the source map into the destination.

assert (destination != null) : "You must provide a destination map to merge into.";
if (source == null)
    return;
for (String key : source.keySet()) {
    if (destination.get(key) != null) {
    } else {
        destination.put(key, source.get(key));