Convert a list of items into a Set. - Java java.util

Java examples for java.util:List Convert

Description

Convert a list of items into a Set.

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{
    /**//from w ww  .  jav a2  s .c o m
     * Convert a list of items into a Set. An empty set is returned if items is null
     * 
     * @param <T>
     *            Any type of object
     * @param items
     *            A variable length list of items of type T
     * @return Returns a new HashSet<T> or an empty set
     */
    public static final <T> Map<T, T> newInOrderMap(T... items) {
        return addToMap(new LinkedHashMap<T, T>(
                items != null ? items.length / 2 : 0), items);
    }

    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"); //$NON-NLS-1$
            }

            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}", //$NON-NLS-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;
    }
    /**
     * 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();
    }
}

Related Tutorials