Create map from key value pair - Java java.util

Java examples for java.util:Map Value

Description

Create map from key value pair

Demo Code

/*/*  ww w.  ja va2 s.  c  o  m*/
 * Copyright 2010 the original author or authors.
 *
 * 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.
 */
//package com.java2s;

import java.util.*;

public class Main {
    public static <K, V> Map<K, V> mapOf(K key, V value) {
        Map<K, V> map = new LinkedHashMap<K, V>();
        map.put(key, value);
        return map;
    }

    public static <K, V> Map<K, V> mapOf(K key, V value, K key2, V value2) {
        Map<K, V> map = new LinkedHashMap<K, V>();
        map.put(key, value);
        map.put(key2, value2);
        return map;
    }

    public static <K, V> Map<K, V> mapOf(K key, V value, K key2, V value2,
            K key3, V value3) {
        Map<K, V> map = new LinkedHashMap<K, V>();
        map.put(key, value);
        map.put(key2, value2);
        map.put(key3, value3);
        return map;
    }

    public static <K, V> Map<K, V> mapOf(K key, V value, K key2, V value2,
            K key3, V value3, K key4, V value4) {
        Map<K, V> map = new LinkedHashMap<K, V>();
        map.put(key, value);
        map.put(key2, value2);
        map.put(key3, value3);
        map.put(key4, value4);
        return map;
    }

    public static <K, V> Map<K, V> mapOf(K key, V value, K key2, V value2,
            K key3, V value3, K key4, V value4, K key5, V value5) {
        Map<K, V> map = new LinkedHashMap<K, V>();
        map.put(key, value);
        map.put(key2, value2);
        map.put(key3, value3);
        map.put(key4, value4);
        map.put(key5, value5);
        return map;
    }
}

Related Tutorials