Java Map Merge merge(final Map... maps)

Here you can find the source of merge(final Map... maps)

Description

Merges specified maps into one new map and returns it.

License

Open Source License

Parameter

Parameter Description
maps maps to merge into new one
K key type
V value type

Return

new map containing all provided maps merged into it

Declaration

public static <K, V> HashMap<K, V> merge(final Map<K, V>... maps) 

Method Source Code


//package com.java2s;
/*//from  www .  j  a v a2 s.c o  m
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**
     * Merges specified maps into one new map and returns it.
     *
     * @param maps maps to merge into new one
     * @param <K>  key type
     * @param <V>  value type
     * @return new map containing all provided maps merged into it
     */
    public static <K, V> HashMap<K, V> merge(final Map<K, V>... maps) {
        // Preparing new map size
        int size = 0;
        for (final Map<K, V> map : maps) {
            if (map != null) {
                size += map.size();
            }
        }

        // Creating and filling new map
        final HashMap<K, V> merged = new HashMap<K, V>(size);
        for (final Map<K, V> map : maps) {
            if (map != null) {
                merged.putAll(map);
            }
        }
        return merged;
    }
}

Related

  1. deepMerge(Map dst, Map src)
  2. deepMerge(Map m1, Map m2)
  3. deepMerge(Map original, Map newMap)
  4. merge(final Map lhs, final Map rhs)
  5. merge(final Map map1, final Map map2)
  6. merge(final Map> targetContext, final Map> newContext)
  7. merge(List> list)
  8. merge(Map map1, Map map2)
  9. merge(Map options, Map addition)