Java Map Merge merge(Map map1, Map map2)

Here you can find the source of merge(Map map1, Map map2)

Description

Creates a new HashMap that has all of the elements of map1 and map2 (on key collision, the latter override the former).

License

Apache License

Parameter

Parameter Description
map1 the fist hashmap to merge
map2 the second hashmap to merge

Return

new hashmap

Declaration

public static HashMap merge(Map map1, Map map2) 

Method Source Code

//package com.java2s;
/**/*w ww .  jav a 2  s.c o  m*/
 * Copyright (C) 2009 GIP RECIA http://www.recia.fr
 * @Author (C) 2009 GIP RECIA <contact@recia.fr>
 * @Contributor (C) 2009 SOPRA http://www.sopragroup.com/
 * @Contributor (C) 2011 Pierre Legay <pierre.legay@recia.fr>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.HashMap;
import java.util.Map;

public class Main {
    /**
     * Creates a new <code>HashMap</code> that has all of the elements
     * of <code>map1</code> and <code>map2</code> (on key collision, the latter
     * override the former).
     *
     * @param map1 the fist hashmap to merge
     * @param map2 the second hashmap to merge
     * @return new hashmap
     */
    public static HashMap merge(Map map1, Map map2) {
        HashMap retval = new HashMap(calcCapacity(map1.size() + map2.size()));

        retval.putAll(map1);
        retval.putAll(map2);

        return retval;
    }

    /**
     * Calculates initial capacity needed to hold <code>size</code> elements in
     * a HashMap or Hashtable without forcing an expensive increase in internal
     * capacity. Capacity is based on the default load factor of .75.
     * <p>
     * Usage: <code>Map map = new HashMap(HashMapUtils.calcCapacity(10));<code>
     * </p>
     * @param size the number of items that will be put into a HashMap
     * @return initial capacity needed
     */
    public static final int calcCapacity(int size) {
        return ((size * 4) + 3) / 3;
    }
}

Related

  1. merge(final Map lhs, final Map rhs)
  2. merge(final Map map1, final Map map2)
  3. merge(final Map... maps)
  4. merge(final Map> targetContext, final Map> newContext)
  5. merge(List> list)
  6. merge(Map options, Map addition)
  7. merge(Map into, Map with)
  8. merge(Map left, Map right)
  9. merge(Map map, K key, V value)