Java Map Create map(K key1, V value1, Object... objects)

Here you can find the source of map(K key1, V value1, Object... objects)

Description

Creates a map out of an infinite amount of parameters.

License

Open Source License

Return

A .

Declaration

public static <K, V> Map<K, V> map(K key1, V value1, Object... objects) 

Method Source Code


//package com.java2s;
/*/*ww w  . j a  v  a2 s  .c  o m*/
 *  Prison is a Minecraft plugin for the prison game mode.
 *  Copyright (C) 2017 The Prison Team
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
    /**
     * Creates a map out of an infinite amount of parameters. Every odd parameter (1, 3, 5, etc.) is a
     * key, and every even parameter (2, 4, 6, etc.) is a value.
     *
     * @return A {@link Map}.
     */
    public static <K, V> Map<K, V> map(K key1, V value1, Object... objects) {
        Map<K, V> ret = new LinkedHashMap<>();

        ret.put(key1, value1);

        Iterator<Object> iter = Arrays.asList(objects).iterator();
        while (iter.hasNext()) {
            K key = (K) iter.next();
            V value = (V) iter.next();
            ret.put(key, value);
        }

        return ret;
    }
}

Related

  1. map(int initialCapacity)
  2. map(K key, V value)
  3. map(K key, V value, Object... keysValues)
  4. map(K key, V value, Object... moreKeysAndValues)
  5. map(K key0, V value0)
  6. map(K[] keys, V[] values)
  7. map(M map, K key, V value)
  8. map(Map map)
  9. map(Map map, Object key)