Java Map Merge mergeMaps(Map destination, Map source)

Here you can find the source of mergeMaps(Map destination, Map source)

Description

Merges the source map into the destination.

License

Open Source License

Parameter

Parameter Description
destination a parameter
source a parameter

Declaration

public static void mergeMaps(Map<String, String> destination, Map<String, String> source) 

Method Source Code

//package com.java2s;
/*//w  w w .ja  v  a2  s .c  o m
 * FrontlineSMS <http://www.frontlinesms.com>
 * Copyright 2007, 2008 kiwanja
 * 
 * This file is part of FrontlineSMS.
 * 
 * FrontlineSMS is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * FrontlineSMS 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 Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with FrontlineSMS. If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Map;

public class Main {
    /**
     * <p>
     * Merges the source map into the destination. Values in the destination
     * take precedence - they will not be overridden if the same key occurs in
     * both destination and source.
     * </p>
     * <p>
     * If a <code>null</code> source is provided, this method does nothing; if a
     * <code>null</code> destination is provided, a {@link NullPointerException}
     * will be thrown.
     * 
     * @param destination
     * @param source
     */
    public static void mergeMaps(Map<String, String> destination, Map<String, String> source) {
        assert (destination != null) : "You must provide a destination map to merge into.";

        // If there is nothing to merge, just return.
        if (source == null)
            return;

        for (String key : source.keySet()) {
            if (destination.get(key) != null) {
                // key already present in language bundle - ignoring
            } else {
                // this key does not appear in the language bundle, so add it
                // with the value from the map
                destination.put(key, source.get(key));
            }
        }
    }
}

Related

  1. mergeMaps(Map dest, Map inserts)
  2. mergeMaps(Map into, Map from, boolean override)
  3. mergeMaps(Map m1, Map m2)
  4. mergeMaps(Map map1, Map map2)
  5. mergeMaps(Map defaultMap, Map customMap)
  6. mergeMaps(Map map1, Map map2)
  7. mergeMapsIgnoreDuplicateKeys( Map first, Map second)
  8. mergeMapWithAdd(Map target, Map source)
  9. mergeNestableMap(Map original, Map additional)