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

MapmergeMaps(Map map1, Map map2)
merge Maps
final Map<String, T> res = new HashMap<>();
res.putAll(map1);
res.putAll(map2);
return res;
MapmergeMapsIgnoreDuplicateKeys(Map first, Map second)
Generic map merge logic
for (Map.Entry<T1, T2> entry : second.entrySet()) {
    T1 key = entry.getKey();
    if (!first.containsKey(key)) {
        first.put(key, entry.getValue());
return first;
MapmergeMapWithAdd(Map target, Map source)
merge Map With Add
for (Entry<? extends TKey, ? extends Integer> entry2 : source.entrySet()) {
    TKey key2 = entry2.getKey();
    int val2 = entry2.getValue();
    if (target.containsKey(key2)) {
        int val1 = target.get(key2);
        target.put(key2, val1 + val2);
    } else {
        target.put(key2, val2);
...
MapmergeNestableMap(Map original, Map additional)
Deep-merge a map into another map returning a result map.
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;
...
MapmergeNsPrefixes(final Map prioritaryPrefixes, final Map additionalPrefixes)
Creates a new Map object containing all prefixes from both specified maps.
Map<String, String> mergedPrefixes = new HashMap<String, String>();
mergedPrefixes.putAll(additionalPrefixes);
mergedPrefixes.putAll(prioritaryPrefixes); 
return mergedPrefixes;
voidmergeOptions(Map> ssio, Map> allOptions)
merge Options
ssio.forEach((k, v) -> {
    if (v != null) {
        Map<String, String> options = allOptions.get(k);
        if (options == null) {
            allOptions.put(k, v);
        } else {
            options.putAll(v);
});
voidmergePermMap(Map> permMap, Map> subPermMap)
merge Perm Map
Set<Entry<String, Set<T>>> subEntrySet = subPermMap.entrySet();
Iterator<Entry<String, Set<T>>> subEntryIterator = subEntrySet.iterator();
while (subEntryIterator.hasNext()) {
    Entry<String, Set<T>> subEntry = subEntryIterator.next();
    String permTypeName = subEntry.getKey();
    Set<T> permValueSet = subEntry.getValue();
    if (permMap.containsKey(permTypeName)) {
        permMap.get(permTypeName).addAll(permValueSet);
...
MapmergePropertiesToMap(Properties properties, Map map, boolean overwrite)
Returns new Map
Map<K, V> resultMap = new HashMap<>();
for (K mapKey : map.keySet()) {
    resultMap.put(mapKey, map.get(mapKey));
for (Object propertyKey : properties.keySet()) {
    if ((overwrite) || (false == resultMap.containsKey((K) propertyKey))) {
        resultMap.put((K) propertyKey, (V) properties.get((K) propertyKey));
return resultMap;
voidmergeResourceBundle(final Map map, final String path)
Merge the resource bundle at the path into the specified map.
final ResourceBundle bundle = ResourceBundle.getBundle(path);
final Enumeration<String> keyEnum = bundle.getKeys();
while (keyEnum.hasMoreElements()) {
    final String key = keyEnum.nextElement();
    final String assignment = bundle.getString(key);
    if (key.startsWith("+")) {
        final String baseKey = key.substring(1);
        final String initialAssignment = (String) map.get(baseKey);
...
StringmergeTemplate(String template, Map segments)
Takes a template and replaces the segment keys with the segment values, keys should not have {} around them yet as these will be added around each key in the segments map
if (template == null || "".equals(template) || segments == null) {
    throw new IllegalArgumentException(
            "Cannot operate on null template/segments, template must not be empty");
int vars = 0;
char[] chars = template.toCharArray();
for (int i = 0; i < chars.length; i++) {
    if (chars[i] == '{') {
...