Java Map Create map(K[] keys, V[] values)

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

Description

Creates a mapping from two arrays, one with keys, one with values.

License

Open Source License

Parameter

Parameter Description
K Data type of the keys.
V Data type of the values.
keys Array containing the keys.
values Array containing the values.

Return

Map with keys and values from the specified arrays.

Declaration

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

Method Source Code


//package com.java2s;
/*//from   www .  j  a v a  2 s  .c  o  m
 * VectorGraphics2D: Vector export for Java(R) Graphics2D
 *
 * (C) Copyright 2010-2011 Erich Seifert <dev[at]erichseifert.de>
 *
 * This file is part of VectorGraphics2D.
 *
 * VectorGraphics2D 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.
 *
 * VectorGraphics2D 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 VectorGraphics2D.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Creates a mapping from two arrays, one with keys, one with values.
     * @param <K> Data type of the keys.
     * @param <V> Data type of the values.
     * @param keys Array containing the keys.
     * @param values Array containing the values.
     * @return Map with keys and values from the specified arrays.
     */
    public static <K, V> Map<K, V> map(K[] keys, V[] values) {
        // Check for valid parameters
        if (keys.length != values.length) {
            throw new IllegalArgumentException("Number of keys and values is different. " + "Cannot create map.");
        }
        // Fill map with keys and values
        Map<K, V> map = new HashMap<K, V>();
        for (int i = 0; i < keys.length; i++) {
            K key = keys[i];
            V value = values[i];
            map.put(key, value);
        }
        return map;
    }
}

Related

  1. map(K key, V value)
  2. map(K key, V value, Object... keysValues)
  3. map(K key, V value, Object... moreKeysAndValues)
  4. map(K key0, V value0)
  5. map(K key1, V value1, Object... objects)
  6. map(M map, K key, V value)
  7. map(Map map)
  8. map(Map map, Object key)
  9. map(Map.Entry... entries)