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

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

Description

new Hash Map

License

Apache License

Declaration

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

Method Source Code


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

import java.util.HashMap;

public class Main {
    public static <K, V> HashMap<K, V> newHashMap(K[] keys, V[] values) {
        if (keys.length > values.length) {
            throw new IllegalArgumentException("Values is less than keys");
        }//  w  ww  .j av  a  2s  . c  om
        HashMap<K, V> map = new HashMap<>();
        for (int i = 0; i < keys.length; i++) {
            map.put(keys[i], values[i]);
        }
        return map;
    }
}

Related

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