Convert a list of items into a Map where the key and value types are specified. - Java java.util

Java examples for java.util:List Convert

Description

Convert a list of items into a Map where the key and value types are specified.

Demo Code


import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main{
    public static void main(String[] argv){
        Class keyType = String.class;
        Class valueType = String.class;
        Object items = "java2s.com";
        System.out.println(newTypedMap(keyType,valueType,items));
    }/*w w  w .j a v a2  s . c o  m*/

    public static final <T, U> Map<T, U> newTypedMap(Class<T> keyType,
            Class<U> valueType, Object... items) {
        Map<T, U> result;

        if (keyType != null && valueType != null
                && !ArrayUtility.isEmpty(items)) {
            result = new HashMap<T, U>();
            addToMap(keyType, valueType, result, items);
        } else {
            result = Collections.emptyMap();
        }

        return result;
    }
    /**
     * This is a convenience method that returns true if the specified collection is null or empty
     * 
     * @param <T>
     *            Any type of object
     * @param collection
     * @return
     */
    public static <T> boolean isEmpty(Collection<T> collection) {
        return collection == null || collection.isEmpty();
    }
    /**
     * This is a convenience method that returns true if the specified map is null or empty
     * 
     * @param <T>
     *            any type of key
     * @param <U>
     *            any type of value
     * @param map
     * @return
     */
    public static <T, U> boolean isEmpty(Map<T, U> map) {
        return map == null || map.isEmpty();
    }

    public static final <T, U> void addToMap(Class<T> keyType,
            Class<U> valueType, Map<T, U> map, Object... items) {
        if (keyType != null && valueType != null && map != null
                && !ArrayUtility.isEmpty(items)) {
            if (items.length % 2 != 0) {
                throw new IllegalArgumentException(
                        "Length of list of items must be multiple of 2");
            }

            for (int i = 0; i < items.length; i += 2) {
                Object keyObject = items[i];
                T key;
                if (keyType.isAssignableFrom(keyObject.getClass())) {
                    key = keyType.cast(keyObject);
                } else {
                    // @formatter:off
                    String message = MessageFormat.format(
                            "Key {0} was not of the expected type: {1}", 
                            i, keyType);
                    // @formatter:on
                    throw new IllegalArgumentException(message);
                }

                Object valueObject = items[i + 1];
                U value;
                if (valueObject == null) {
                    value = null;
                    map.put(key, value);
                } else if (valueType.isAssignableFrom(valueObject
                        .getClass())) {
                    value = valueType.cast(valueObject);

                    map.put(key, value);
                } else {
                    String message = MessageFormat.format(
                            "Value {0} was not of the expected type: {1}", 
                            i + 1, valueType);

                    throw new IllegalArgumentException(message);
                }
            }
        }
    }

    public static final <T, U extends T> Map<T, T> addToMap(Map<T, T> map,
            U... items) {
        if (map != null && items != null) {
            if (items.length % 2 != 0) {
                throw new IllegalArgumentException(
                        "Length of list of items must be multiple of 2"); //$NON-NLS-1$
            }
            for (int i = 0; i < items.length; i += 2) {
                map.put(items[i], items[i + 1]);
            }
        }

        return map;
    }
}

Related Tutorials