Java Map Create map(K key, V value, Object... keysValues)

Here you can find the source of map(K key, V value, Object... keysValues)

Description

Creates the HashMap of Integers

License

Apache License

Parameter

Parameter Description
key first key of the map
value first value of the map
keysValues pairs of key value Example: sMap(1, 2, 2, 4) will create {1 -> 2, 2 -> 4}

Return

new HashMap of String -> String

Declaration

@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> map(K key, V value, Object... keysValues) 

Method Source Code


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

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**// www.j  av  a2 s  .c o  m
     * Creates the HashMap of Integers
     *
     * @param key        first key of the map
     * @param value      first value of the map
     * @param keysValues pairs of key value
     *                   Example: sMap(1, 2, 2, 4) will create {1 -> 2, 2 -> 4}
     * @return new HashMap of String -> String
     */
    @SuppressWarnings("unchecked")
    public static <K, V> Map<K, V> map(K key, V value, Object... keysValues) {
        Map<K, V> res = new HashMap<>();
        res.put(key, value);
        if (keysValues.length > 0) {
            if (keysValues.length % 2 > 0) {
                throw new IllegalArgumentException("Arguments count must be even!");
            }
            for (int i = 0; i < keysValues.length; i += 2) {
                K k = (K) keysValues[i];
                V v = (V) keysValues[i + 1];
                res.put(k, v);
            }
        }
        return res;
    }
}

Related

  1. map(final Entry... entries)
  2. map(final Object... keysAndValues)
  3. map(final Properties props)
  4. map(int initialCapacity)
  5. map(K key, V value)
  6. map(K key, V value, Object... moreKeysAndValues)
  7. map(K key0, V value0)
  8. map(K key1, V value1, Object... objects)
  9. map(K[] keys, V[] values)