Java Map Merge mergeNestableMap(Map original, Map additional)

Here you can find the source of mergeNestableMap(Map original, Map additional)

Description

Deep-merge a map into another map returning a result map.

License

Open Source License

Parameter

Parameter Description
original nestable Map of entries to retain and not overwrite
additional nestable Map of entries to add to the original

Return

merge of original and additional nestable map

Declaration

public static Map<String, Object> mergeNestableMap(Map<String, Object> original,
        Map<String, Object> additional) 

Method Source Code

//package com.java2s;
/**************************************************************************************
 * Copyright (C) 2008 EsperTech, Inc. All rights reserved.                            *
 * http://esper.codehaus.org                                                          *
 * http://www.espertech.com                                                           *
 * ---------------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the GPL license       *
 * a copy of which has been included with this distribution in the license.txt file.  *
 **************************************************************************************/

import java.util.*;

public class Main {
    /**/*from  w w  w  .  jav  a 2 s.  c o  m*/
     * Deep-merge a map into another map returning a result map.
     * <p>
     * Copies all values present in the original map to a new map,
     * adding additional value present in the second map passed in,
     * ignoring same-key values in the second map that are present in the original.
     * <p>
     * If the value is a Map itself, repeats the operation on the Map value.
     * @param original nestable Map of entries to retain and not overwrite
     * @param additional nestable Map of entries to add to the original
     * @return merge of original and additional nestable map
     */
    public static Map<String, Object> mergeNestableMap(Map<String, Object> original,
            Map<String, Object> additional) {
        Map<String, Object> result = new LinkedHashMap<String, Object>(original);

        for (Map.Entry<String, Object> additionalEntry : additional.entrySet()) {
            String name = additionalEntry.getKey();
            Object additionalValue = additionalEntry.getValue();

            Object originalValue = original.get(name);

            Object newValue;
            if ((originalValue instanceof Map) && (additionalValue instanceof Map)) {
                Map<String, Object> innerAdditional = (Map<String, Object>) additionalValue;
                Map<String, Object> innerOriginal = (Map<String, Object>) originalValue;
                newValue = mergeNestableMap(innerOriginal, innerAdditional);
                result.put(name, newValue);
                continue;
            }

            if (original.containsKey(name)) {
                continue;
            }
            result.put(name, additionalValue);
        }
        return result;
    }
}

Related

  1. mergeMaps(Map defaultMap, Map customMap)
  2. mergeMaps(Map destination, Map source)
  3. mergeMaps(Map map1, Map map2)
  4. mergeMapsIgnoreDuplicateKeys( Map first, Map second)
  5. mergeMapWithAdd(Map target, Map source)
  6. mergeNsPrefixes(final Map prioritaryPrefixes, final Map additionalPrefixes)
  7. mergeOptions(Map> ssio, Map> allOptions)
  8. mergePermMap(Map> permMap, Map> subPermMap)
  9. mergePropertiesToMap(Properties properties, Map map, boolean overwrite)