Java HashMap Create newHashMap(K[] keys, V[] values)

Here you can find the source of newHashMap(K[] keys, V[] values)

Description

Create a new hash map and fills it from the given keys and values (keys[index] -> values[index].

License

Apache License

Parameter

Parameter Description
keys The array of keys.
values The array of values.

Return

A map that contains for each key element in the keys array a value from the values array at the same index.

Declaration

public static <K, V> Map<K, V> newHashMap(K[] keys, V[] values) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**//from  w  w  w.  j  a  v a2 s  . c o  m
     * Create a new hash map and fills it from the given keys and values (keys[index] -> values[index].
     * 
     * @param keys The array of keys.
     * @param values The array of values.
     * @return A map that contains for each key element in the keys array a value from the values array at the same index.
     */
    public static <K, V> Map<K, V> newHashMap(K[] keys, V[] values) {
        Map<K, V> map = new HashMap<K, V>();
        if (keys == null || values == null || keys.length != values.length) {
            throw new IllegalArgumentException("keys and values must be non-null and have the same size.");
        }
        for (int i = 0; i < keys.length; i++) {
            map.put(keys[i], values[i]);
        }
        return map;
    }
}

Related

  1. newHashMap(final K key, final V value)
  2. newHashMap(int capacity)
  3. newHashMap(int size)
  4. newHashMap(int size)
  5. newHashMap(int size)
  6. newHashMap(K[] keys, V[] values)
  7. newHashMap(Map.Entry... entries)
  8. newHashMap(Map m)
  9. newHashMap(Object... kv)