Here you can find the source of newHashMap(final K key, final V value)
Parameter | Description |
---|---|
key | key to add |
value | value to add |
K | key type |
V | value type |
public static <K, V> HashMap<K, V> newHashMap(final K key, final V value)
//package com.java2s; /*/*from ww w. j ava 2 s. c o m*/ * This file is part of WebLookAndFeel library. * * WebLookAndFeel library 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. * * WebLookAndFeel library 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 WebLookAndFeel library. If not, see <http://www.gnu.org/licenses/>. */ import java.util.HashMap; public class Main { /** * Returns newly created HashMap with the specified key and value pair added. * * @param key key to add * @param value value to add * @param <K> key type * @param <V> value type * @return newly created HashMap */ public static <K, V> HashMap<K, V> newHashMap(final K key, final V value) { final HashMap<K, V> map = new HashMap<K, V>(1); map.put(key, value); return map; } /** * Returns newly created HashMap with the specified key and value pairs added. * * @param objects key-value pairs * @param <K> key type * @param <V> value type * @return newly created HashMap */ public static <K, V> HashMap<K, V> newHashMap(final Object... objects) { if (objects != null && objects.length > 0) { if (objects.length % 2 == 0) { final HashMap<K, V> map = new HashMap<K, V>(1); for (int i = 0; i < objects.length; i += 2) { map.put((K) objects[i], (V) objects[i + 1]); } return map; } else { throw new RuntimeException("Amount of key-value objects must be even"); } } else { return new HashMap<K, V>(0); } } }