Creates a list by taking an input list and adding elements to the list. - Java Collection Framework

Java examples for Collection Framework:List

Description

Creates a list by taking an input list and adding elements to the list.

Demo Code


//package com.java2s;
import java.util.ArrayList;

import java.util.List;

public class Main {
    /**//from w w w  . ja  v a  2 s  .co  m
     * Creates a list by taking an input list and adding elements to the list.
     *
     * @param <T> Type of the list.
     * @param list The input list.
     * @param elems The elements to add to the list.
     *
     * @return The modified input list.
     */
    public static <T> List<T> list(final List<T> list, final T... elems) {
        for (final T elem : elems) {
            list.add(elem);
        }

        return list;
    }

    /**
     * Creates a list from a number of elements.
     *
     * @param <T> The type of the elements.
     * @param elems The elements to add to the list.
     *
     * @return The list that contains the input elements.
     */
    public static <T> List<T> list(final T... elems) {
        final List<T> list = new ArrayList<T>();

        for (final T elem : elems) {
            list.add(elem);
        }

        return list;
    }
}

Related Tutorials