Add a varargs list of items into a map. - Java java.util

Java examples for java.util:Map Creation

Description

Add a varargs list of items into a map.

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 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"); 
            }/*from  ww  w  .j a  va2s .c  o  m*/

            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 {
                    // @formatter:off
                    String message = MessageFormat.format(
                            "Value {0} was not of the expected type: {1}", //$NON-NLS-1$
                            i + 1, valueType);
                    // @formatter:on
                    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;
    }

    public static <T> boolean isEmpty(Collection<T> collection) {
        return collection == null || collection.isEmpty();
    }

    public static <T, U> boolean isEmpty(Map<T, U> map) {
        return map == null || map.isEmpty();
    }
}

Related Tutorials