Java Collection to List toList(Collection c)

Here you can find the source of toList(Collection c)

Description

to List

License

Open Source License

Parameter

Parameter Description
c a parameter

Return

c if c is a or, otherwise, an ArrayList containing the elements of c

Declaration

public static <V, T extends V> List<V> toList(Collection<T> c) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import java.util.List;

public class Main {
    /**/* w  w w . jav  a 2 s  .  co m*/
     * @param c
     * @return c if c is a {@link List} or, otherwise, an ArrayList containing
     *         the elements of c
     */
    public static <V, T extends V> List<V> toList(Collection<T> c) {
        return asList(c);
    }

    /**
     * @param c
     * @return c if c is a {@link List} or, otherwise, a new List containing
     *         the elements of c
     */
    public static <V, T extends V> List<V> asList(Collection<T> c) {
        if (c instanceof List)
            return (List<V>) c;
        List<V> list = new ArrayList<V>(c);
        return list;
    }

    public static <V, T> List<V> asList(Object o, Class<V> cls) {
        if (o == null)
            return null;
        if (o instanceof Collection) {
            return asList((Collection<?>) o, cls);
        }
        return asList(newList(o), cls);
    }

    /**
     * @param c
     * @param cls
     * @return a new {@link List} containing
     *         the elements of c cast to type V
     */
    public static <V, T> ArrayList<V> asList(Collection<T> c, Class<V> cls) {
        ArrayList<V> list = new ArrayList<V>();
        for (T t : c) {
            if (t == null || cls == null || cls.isAssignableFrom(t.getClass())) {
                try {
                    V v = cls.cast(t);
                    list.add(v);
                } catch (ClassCastException e) {
                }
            }
        }
        return list;
    }

    /**
     * Creates a new {@link ArrayList} and inserts the arguments, {@code ts}.
     * @param ts
     * @return the new {@link ArrayList}
     */
    public static <T> ArrayList<T> newList(T... ts) {
        ArrayList<T> newList = new ArrayList<T>();
        if (ts != null && ts.length > 0) {
            newList.addAll(Arrays.asList(ts));
        }
        return newList;
    }

    public static <T, C extends Collection<T>> C add(C coll, T... items) {
        return plus(coll, items);
    }

    public static <T, C extends Collection<T>, D extends Collection<T>> C add(C coll, D items) {
        return plus(coll, items);
    }

    /**
     * A potentially more efficient addAll() for unordered Collections.
     * @param coll1
     * @param coll2
     * @return the longer of the two collections after adding the shorter to the longer.  
     */
    public static <T, C extends Collection<T>> C addAll(Collection<T> coll1, Collection<T> coll2) {
        if (coll1 == null)
            return (C) coll2;
        if (coll2 == null)
            return (C) coll1;

        Collection<T> cSmaller, cBigger;
        if (coll1.size() < coll2.size()) {
            cSmaller = coll1;
            cBigger = coll2;
        } else {
            cSmaller = coll2;
            cBigger = coll1;
        }
        try {
            cBigger.addAll(cSmaller);
            return (C) cBigger;
        } catch (UnsupportedOperationException e) {
        }
        try {
            cSmaller.addAll(cBigger);
            return (C) cSmaller;
        } catch (UnsupportedOperationException e) {
        }
        ArrayList<T> newList = new ArrayList<T>(cBigger);
        newList.addAll(cSmaller);
        return (C) newList;
    }

    public static <T, C extends Collection<T>> C plus(C coll, T... items) {
        coll.addAll(newList(items));
        return coll;
    }

    public static <T, C extends Collection<T>, D extends Collection<T>> C plus(C coll, D items) {
        coll.addAll(items);
        return coll;
    }
}

Related

  1. toList(Collection collection)
  2. toList(Collection c)
  3. toList(Collection collection)
  4. toList(Collection lines)
  5. toList(Collection c)
  6. toList(Collection collection)