cast Collection to List - Java java.util

Java examples for java.util:Collection to List

Description

cast Collection to List

Demo Code


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

public class Main {
    public static void main(String[] argv) {
        Class clazz = String.class;
        Collection c = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(castList(clazz, c));
    }//from www. j a  v a 2  s .  c om

    public static <T> List<T> castList(Class<? extends T> clazz,
            Collection<?> c) {
        List<T> r = new ArrayList<T>(c.size());
        for (Object o : c)
            r.add(clazz.cast(o));
        return r;
    }
}

Related Tutorials