Convert Collection to ArrayList - Java Collection Framework

Java examples for Collection Framework:Array Convert

Description

Convert Collection to ArrayList

Demo Code


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

public class Main {
    public static void main(String[] argv) {
        Collection col = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(toArrayList(col));
    }//from  ww  w  . ja v a  2  s .  com

    public static <K> ArrayList<K> toArrayList(Collection<K> col) {
        if (col == null) {
            return new ArrayList<K>();
        }
        ArrayList<K> result = new ArrayList<K>();
        for (K k : col) {
            result.add(k);
        }
        return result;
    }
}

Related Tutorials