convert to ArrayList - Java Collection Framework

Java examples for Collection Framework:Array Convert

Description

convert to ArrayList

Demo Code


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

import java.util.Collection;

import java.util.Set;

public class Main {
    /**//w  w  w  . j a  va2 s .co  m
     * @param array A collection of elements to be included in the returned ArrayList
     * @return An ArrayList that includes all the elements passed in via the array parameter
     */
    public static <T> ArrayList<T> toArrayList(final T... array) {
        final ArrayList<T> retValue = new ArrayList<T>();
        for (final T item : array)
            retValue.add(item);
        return retValue;
    }

    /**
     * @param set The Set to be converted to an ArrayList
     * @return An ArrayList containing the elements in the set
     */
    public static <T> ArrayList<T> toArrayList(final Set<T> set) {
        final ArrayList<T> retValue = new ArrayList<T>();
        for (final T item : set)
            retValue.add(item);
        return retValue;
    }

    /**
     * @param items The Collection of items to be converted to an ArrayList
     * @return An ArrayList containing the elements in the set
     */
    public static <T> ArrayList<T> toArrayList(final Collection<T> items) {
        final ArrayList<T> retValue = new ArrayList<T>();
        for (final T item : items)
            retValue.add(item);
        return retValue;
    }
}

Related Tutorials