Creates and returns a map populated with the keyValuesSets where the value held by the tuples are they key and value in that order. - Java java.util

Java examples for java.util:Map Value

Description

Creates and returns a map populated with the keyValuesSets where the value held by the tuples are they key and value in that order.

Demo Code


//package com.java2s;
import java.util.HashMap;

import java.util.Map;

public class Main {
    /**//from  www .  j  av a2  s .co m
     * Creates and returns a map populated with the keyValuesSets where
     * the value held by the tuples are they key and value in that order.
     * 
     * @param keys K[]
     * @param values V[]
     * @return Map
     */
    public static <K, V> Map<K, V> mapFrom(K[] keys, V[] values) {
        if (keys.length != values.length) {
            throw new RuntimeException(
                    "mapFrom keys and values arrays have different sizes");
        }
        Map<K, V> map = new HashMap<K, V>(keys.length);
        for (int i = 0; i < keys.length; i++) {
            map.put(keys[i], values[i]);
        }
        return map;
    }
}

Related Tutorials