Create an Object List from the supplied objects. - Java java.util

Java examples for java.util:List Creation

Description

Create an Object List from the supplied objects.

Demo Code


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

public class Main {
    /**//from  w  w w .  j  a  v a 2  s. com
     * Create an Object {@link List} from the supplied objects.
     * @param objects The objects to be added to the list.
     * @return The {@link List}.
     */
    public static <T> List<T> toList(T... objects) {
        List<T> theList = new ArrayList<T>();
        addToCollection(theList, objects);
        return theList;
    }

    /**
     * Create an Object {@link List} from the supplied Enumeration of objects.
     * @param objects The objects to be added to the list.
     * @return The {@link List}.
     */
    public static <T> List<T> toList(Enumeration<T> objects) {
        List<T> theList = new ArrayList<T>();
        while (objects.hasMoreElements()) {
            theList.add(objects.nextElement());
        }
        return theList;
    }

    private static <T> void addToCollection(Collection<T> theCollection,
            T... objects) {
        for (T object : objects) {
            theCollection.add(object);
        }
    }
}

Related Tutorials