Java List Value Add All addToList(List list, U... items)

Here you can find the source of addToList(List list, U... items)

Description

Add a varargs list of items to the specified list.

License

Open Source License

Parameter

Parameter Description
list A list to which items will be added
items A list of items to add

Declaration

public static final <T, U extends T> List<T> addToList(List<T> list, U... items) 

Method Source Code

//package com.java2s;
/**//from  w w w.j  a v  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 {
    /**
     * 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. addToList(List list, I item)
  2. AddToList(List tips, String tip)
  3. addToList(List dest, T src)
  4. addToList(List list, T value)
  5. addToList(List list, T[] array)