Java Map Create createMap(Iterable> entries)

Here you can find the source of createMap(Iterable> entries)

Description

Initialize a map using a variable number of Entry objects as input

License

Open Source License

Parameter

Parameter Description
entries a parameter

Declaration

public static <K, V> Map<K, V> createMap(Iterable<? extends Entry<K, V>> entries) 

Method Source Code

//package com.java2s;

import java.util.HashMap;

import java.util.Map;
import java.util.Map.Entry;

public class Main {
    /**/*w ww  . j a  v a 2s . co  m*/
     * Simple utility to initialize a map with one key/value pair
     * 
     * @param <K>
     * @param <V>
     * @param key
     * @param value
     * @return
     */
    public static <K, V> Map<K, V> createMap(K key, V value) {
        Map<K, V> map = new HashMap<K, V>();
        map.put(key, value);
        return map;
    }

    /**
     * Initialize a map using a variable number of {@link Entry} objects as
     * input
     * 
     * @param entries
     * @return
     */
    public static <K, V> Map<K, V> createMap(Iterable<? extends Entry<K, V>> entries) {
        Map<K, V> map = new HashMap<K, V>();
        for (Entry<K, V> entry : entries)
            map.put(entry.getKey(), entry.getValue());
        return map;
    }

    /**
     * Simple utility to initialize a map with two key/value pairs.
     * 
     * @param <K>
     * @param <V>
     * @param key1
     * @param value1
     * @param key2
     * @param value2
     * @return
     */
    public static <K, V> Map<K, V> createMap(K key1, V value1, K key2, V value2) {
        if (key1.equals(key2))
            throw new IllegalArgumentException(
                    "Key1 = Key2, value1 will be overwritten! The createMap method requires unique keys.");
        Map<K, V> map = new HashMap<K, V>();
        map.put(key1, value1);
        map.put(key2, value2);
        return map;
    }

    /**
     * Simple utility to initialize a mpa with three key/value pairs.
     * 
     * @param <K>
     * @param <V>
     * @param key1
     * @param value1
     * @param key2
     * @param value2
     * @param key3
     * @param value3
     * @return
     */
    public static <K, V> Map<K, V> createMap(K key1, V value1, K key2, V value2, K key3, V value3) {
        Map<K, V> map = new HashMap<K, V>();
        map.put(key1, value1);
        map.put(key2, value2);
        map.put(key3, value3);
        return map;
    }
}

Related

  1. createMap()
  2. createMap()
  3. createMap()
  4. createMap(boolean full)
  5. createMap(int len)
  6. createMap(K k1, V v1)
  7. createMap(K[] keys, V[] values)
  8. createMap(List keys, List values)
  9. createMap(List keys, List values)

    HOME | Copyright © www.java2s.com 2016