Create map from keys and values lists. - Java Collection Framework

Java examples for Collection Framework:Map

Description

Create map from keys and values lists.

Demo Code


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

import java.util.List;
import java.util.Map;

public class Main {
    /**//  w ww  .j a v a2 s . co m
     * Create map from keys and values lists.
     * <p/>
     * <p/>
     * NB: Values with same keys will be overridden.
     *
     * @param keys   Lists of keys
     * @param values Lists of values
     * @param <K>    Keys type
     * @param <V>    Values type
     * @return Map generated from keys and values lists where key assigned with value which
     *         have same index
     * @throws IllegalArgumentException If keys or values are nulls or have different size
     */
    @SuppressWarnings({ "unchecked", "TypeMayBeWeakened" })
    public static <K, V> Map<K, V> createMap(final List<K> keys,
            final List<V> values)

    throws IllegalArgumentException {

        return createMap((K[]) keys.toArray(), (V[]) values.toArray());
    }

    /**
     * Create map from keys and values array.
     * <p/>
     * <p/>
     * NB: Values with same keys will be overridden.
     *
     * @param keys   Array of keys
     * @param values Array of values
     * @param <K>    Keys type
     * @param <V>    Values type
     * @return Map generated from keys and values array's where key assigned with value which
     *         have same index
     * @throws IllegalArgumentException If keys or values are nulls or have different size
     */
    public static <K, V> Map<K, V> createMap(final K[] keys,
            final V[] values) throws IllegalArgumentException {

        if (keys == null || values == null || keys.length != values.length) {
            throw new IllegalArgumentException("Wrong arguments passed");
        }

        final Map<K, V> map = new HashMap<>(keys.length);

        for (int i = 0; i < keys.length; i++) {
            map.put(keys[i], values[i]);
        }

        return map;
    }

    /**
     * Create map from keys and values maps.
     * <p/>
     * <p/>
     * NB: Values with same keys will be overridden.
     *
     * @param keys   Map of keys
     * @param values Map of values
     * @param <I>    Indexes type
     * @param <K>    Keys type
     * @param <V>    Values type
     * @return Map generated from keys and values maps where key assigned with value which
     *         have same index (key for input maps)
     * @throws IllegalArgumentException If keys or values are nulls or have different size
     */
    public static <I, K, V> Map<K, V> createMap(final Map<I, K> keys,
            final Map<I, V> values) {

        if (keys == null || values == null || keys.size() != values.size()) {
            throw new IllegalArgumentException("Wrong arguments passed");
        }

        final Map<K, V> map = new HashMap<>(keys.size());

        for (final I entryKey : keys.keySet()) {
            map.put(keys.get(entryKey), values.get(entryKey));
        }

        return map;
    }
}

Related Tutorials