集册 Java实例教程 从两个数组创建映射,一个具有键,一个具有值。

从两个数组创建映射,一个具有键,一个具有值。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

392
从两个数组创建映射,一个具有键,一个具有值。

/* ******************************************************************************

 * Copyright (c) 2006-2013 XMind Ltd. and others.

 *

 * This file is a part of XMind 3. XMind releases 3 and

 * above are dual-licensed under the Eclipse Public License (EPL),

 * which is available at http://www.eclipse.org/legal/epl-v10.html

 * and the GNU Lesser General Public License (LGPL),

 * which is available at http://www.gnu.org/licenses/lgpl.html

 * See http://www.xmind.net/license.html for details.

 *

 * Contributors:

 *     XMind Ltd. - initial API and implementation

 *******************************************************************************/

//package com.nowjava;//来 自 nowjava - 时代Java


import java.util.LinkedHashMap;


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("Cannot create a Map: " //$NON-NLS-1$

                    + "The number of keys and values differs."); //$NON-NLS-1$/*NowJava.com 提供*/

        }

        
展开阅读全文