Java List Cast castList(Class clazz, Collection collection)

Here you can find the source of castList(Class clazz, Collection collection)

Description

Casts a raw Collection to a generic List .

License

Open Source License

Parameter

Parameter Description
T the type of elements contained in the raw list.
clazz is the Class of the Collection entries.
collection is the raw Collection .

Return

the generic .

Declaration

public static <T> List<T> castList(Class<? extends T> clazz, Collection<?> collection) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Main {
    /**//from  www .j  ava  2 s  . c o m
     * Casts a raw {@link Collection} to a generic {@link List}. Can be used to avoid type safety problems when dealing
     * with raw lists.
     * @param <T> the type of elements contained in the raw list.
     * @param clazz is the {@link Class} of the {@link Collection} entries.
     * @param collection is the raw {@link Collection}.
     * @return the generic {@link List}.
     */
    public static <T> List<T> castList(Class<? extends T> clazz, Collection<?> collection) {
        List<T> genericList = new ArrayList<T>(collection.size());
        for (Object object : collection) {
            genericList.add(clazz.cast(object));
        }
        return genericList;
    }
}

Related

  1. cast(List list, Class klass)
  2. cast(List from)
  3. CAST_LIST(Class className, Object obj)
  4. castList(Class clazz, Collection c)
  5. castList(Class clazz, Collection c)
  6. castList(Class elementType, List list)
  7. castList(Class klass, List list)
  8. castList(Collection c)
  9. castList(final List original)