Java tutorial
//package com.java2s; /* * 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; } }