Java HashMap Create newHashMap(Object... kv)

Here you can find the source of newHashMap(Object... kv)

Description

Construct a new HashMap with a list of key/value pairs as arguments.

License

MIT License

Parameter

Parameter Description
K The type of the keys
V The type of the values
kv An array of (key,value) pairs

Return

An initialized HashMap

Declaration

public static <K, V> HashMap<K, V> newHashMap(Object... kv) 

Method Source Code

//package com.java2s;
/*//w  ww  . ja v  a  2 s.co  m
 * Utils.java - Copyright(c) 2013 Joe Pasqua
 * Provided under the MIT License. See the LICENSE file for details.
 * Created: Jul 8, 2013
 */

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**
     * Construct a new HashMap with a list of key/value pairs as arguments.
     * The arguments are interspersed: k,v,k,v,k,v,...
     * There must be an even number of arguments and each key must be of
     * type K and each value must be of type V.
     * 
     * @param <K>   The type of the keys
     * @param <V>   The type of the values
     * @param kv    An array of (key,value) pairs
     * @return      An initialized HashMap
     */
    public static <K, V> HashMap<K, V> newHashMap(Object... kv) {
        int length = kv.length;
        if (length % 2 != 0)
            throw new IllegalArgumentException("Mismatched keys and values");
        HashMap<K, V> m = new HashMap<>();
        fillMap(m, kv);
        return m;
    }

    /**
     * Construct a new HashMap using the elements of an Enumeration as the keys
     * and an array of objects of type V as the values. The array must be the same
     * length as the number of elements in the enumeration.
     * @param <E>   An Enumeration Type
     * @param <V>   An arbitrary value type
     * @param keys  An array of enums such as the one that would be returned
     *              by SomeEnum.values();
     * @param values    An array of values to go with the enum keys
     * @return      An initialized HashMap
     */
    public static <E extends Enum<E>, V> HashMap<E, V> newHashMap(E[] keys, V[] values) {
        int nKeys = keys.length;
        if (nKeys != values.length)
            throw new IllegalArgumentException("Mismatched keys and values");
        HashMap<E, V> m = new HashMap<>();
        for (int i = 0; i < nKeys; i++) {
            m.put(keys[i], values[i]);
        }
        return m;
    }

    /**
     * Fill an existing map with a list of key/value pairs.
     * The arguments are interspersed: k,v,k,v,k,v,...
     * There must be an even number of arguments and each key must be of
     * type K and each value must be of type V.
     * @param <K>   The type of the keys
     * @param <V>   The type of the values
     * @param m     The map to fill - must not be null
     * @param kv    An array of (key,value) pairs
     */
    public static <K, V> void fillMap(Map<K, V> m, Object... kv) {
        int length = kv.length;
        if (length % 2 != 0)
            throw new IllegalArgumentException("Mismatched keys and values");
        for (int i = 0; i < length;) {
            K key = cast(kv[i++]);
            V value = cast(kv[i++]);
            m.put(key, value);
        }
    }

    /**
     * Perform an unchecked type coercion of an object to a target type. The
     * only real value of this method is that it is declared in one place with
     * the SuppressWarnings directive turned on.
     * @param <E>   The target type
     * @param obj   The object to coerce
     * @return      The supplied object in the specified type
     */
    @SuppressWarnings("unchecked")
    public static <E> E cast(Object obj) {
        return (E) obj;
    }
}

Related

  1. newHashMap(int size)
  2. newHashMap(K[] keys, V[] values)
  3. newHashMap(K[] keys, V[] values)
  4. newHashMap(Map.Entry... entries)
  5. newHashMap(Map m)
  6. newHashMap(T key, U value)
  7. newHashMapWithCapacity(int expectedSize, float loadFactor)
  8. toHashMap( Entry[] entries)
  9. toHashMap(ArrayList list, int beginId)