Here you can find the source of mergeNestableMap(Map
Parameter | Description |
---|---|
original | nestable Map of entries to retain and not overwrite |
additional | nestable Map of entries to add to the original |
public static Map<String, Object> mergeNestableMap(Map<String, Object> original, Map<String, Object> additional)
//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; } }