Here you can find the source of merge(final Map
Parameter | Description |
---|---|
maps | maps to merge into new one |
K | key type |
V | value type |
public static <K, V> HashMap<K, V> merge(final Map<K, V>... maps)
//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; } }