Java Map Create map(K key0, V value0)

Here you can find the source of map(K key0, V value0)

Description

Create a map with one key and a corresponding value.

License

Apache License

Parameter

Parameter Description
key0 the key
value0 the value
K key type
V value type

Return

map with one entry

Declaration

public static <K, V> Map<K, V> map(K key0, V value0) 

Method Source Code

//package com.java2s;
/*/*  w w  w. j  a  va  2  s.c  om*/
 * Copyright Terracotta, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    /**
     * Create a map with one key and a corresponding value.
     *
     * @param key0 the key
     * @param value0 the value
     * @param <K> key type
     * @param <V> value type
     * @return map with one entry
     */
    public static <K, V> Map<K, V> map(K key0, V value0) {
        return Collections.singletonMap(key0, value0);
    }

    /**
     * Create a map with two key/value pairs.
     *
     * @param key0 first key
     * @param value0 first value
     * @param key1 second key
     * @param value1 second value
     * @param <K> key type
     * @param <V> value type
     * @return map with two entries
     */
    public static <K, V> Map<K, V> map(K key0, V value0, K key1, V value1) {
        Map<K, V> map = new HashMap<>(2);
        map.put(key0, value0);
        map.put(key1, value1);
        return map;
    }

    /**
     * Create a map with three key/value pairs.
     *
     * @param key0 first key
     * @param value0 first value
     * @param key1 second key
     * @param value1 second value
     * @param key2 third key
     * @param value2 third value
     * @param <K> key type
     * @param <V> value type
     * @return map with three entries
     */
    public static <K, V> Map<K, V> map(K key0, V value0, K key1, V value1, K key2, V value2) {
        Map<K, V> map = new HashMap<>(2);
        map.put(key0, value0);
        map.put(key1, value1);
        map.put(key2, value2);
        return map;
    }
}

Related

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