Java Map Merge mergeMaps(Map defaultMap, Map customMap)

Here you can find the source of mergeMaps(Map defaultMap, Map customMap)

Description

Recursively merges a custom map into a default map, and returns the merged result.

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
private static Map<String, Object> mergeMaps(Map<String, Object> defaultMap, Map<String, Object> customMap) 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.util.Map;

public class Main {
    /**//from   www .j  a va2  s.  co  m
     * Recursively merges a custom map into a default map, and returns the merged result.
     *
     * <p>All keys in the default map that are also specified in the custom map are overridden with
     * the custom map's value. This runs recursively on all contained maps.
     */
    @SuppressWarnings("unchecked")
    private static Map<String, Object> mergeMaps(Map<String, Object> defaultMap, Map<String, Object> customMap) {
        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));
            } else {
                newValue = customMap.get(key);
            }
            defaultMap.put(key, newValue);
        }
        return defaultMap;
    }
}

Related

  1. mergeMaps(Map mainMap, Map defaultMap, String[] keys, boolean enforceValues)
  2. mergeMaps(Map dest, Map inserts)
  3. mergeMaps(Map into, Map from, boolean override)
  4. mergeMaps(Map m1, Map m2)
  5. mergeMaps(Map map1, Map map2)
  6. mergeMaps(Map destination, Map source)
  7. mergeMaps(Map map1, Map map2)
  8. mergeMapsIgnoreDuplicateKeys( Map first, Map second)
  9. mergeMapWithAdd(Map target, Map source)