Java Array to List newList(T... items)

Here you can find the source of newList(T... items)

Description

Convert a vararg list of items into a List.

License

Open Source License

Parameter

Parameter Description
T Any type of object
items A variable length list of items of type T

Return

Returns a new ArrayList or an empty list

Declaration

public static final <T> List<T> newList(T... items) 

Method Source Code

//package com.java2s;
/**//  ww  w  . j av a  2s.  co m
 * Aptana Studio
 * Copyright (c) 2005-2011 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
 * Please see the license.html included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Convert a vararg list of items into a List. An empty list 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 ArrayList<T> or an empty list
     */
    public static final <T> List<T> newList(T... items) {
        return addToList(new ArrayList<T>(items != null ? items.length : 0), items);
    }

    /**
     * Add a varargs list of items to the specified list. If the list or items array are null, then no action is
     * performed. Note that the destination list has no requirements other than it must be a List of the source item's
     * type. This allows the destination to be used, for example, as an accumulator.<br>
     * <br>
     * Note that this method is not thread safe. Users of this method will need to maintain type safety against the
     * list.
     * 
     * @param list
     *            A list to which items will be added
     * @param items
     *            A list of items to add
     */
    public static final <T, U extends T> List<T> addToList(List<T> list, U... items) {
        if (list != null && items != null) {
            for (int i = 0; i < items.length; i++) {
                list.add(items[i]);
            }
            if (list instanceof ArrayList) {
                ((ArrayList<T>) list).trimToSize();
            }
        }

        return list;
    }

    /**
     * Add a varargs list of items to the specified list. If the list or items array are null, then no action is
     * performed. Note that the destination list has no requirements other than it must be a List of the source item's
     * type. This allows the destination to be used, for example, as an accumulator.<br>
     * <br>
     * Note that this method is not thread safe. Users of this method will need to maintain type safety against the
     * list.
     * 
     * @param list
     *            A list to which items will be added
     * @param items
     *            A list of items to add
     */
    public static final <T, U extends T> List<T> addToList(List<T> list, List<U> items) {
        if (list != null && items != null) {
            list.addAll(items);
        }

        return list;
    }
}

Related

  1. newList(final T... elements)
  2. newList(final T... elements)
  3. newList(int... sizes)
  4. newList(T... elements)
  5. newList(T... elements)
  6. newList(T... list)
  7. newList(T... objects)
  8. newList(T... objs)
  9. newList(T... ts)