Java List Cast castListElem(List list, V target)

Here you can find the source of castListElem(List list, V target)

Description

Creates a new list of the objects cast as the type passed as target.

License

Creative Commons License

Parameter

Parameter Description
list a parameter

Declaration

@SuppressWarnings("unchecked")
public static <T, V> List<V> castListElem(List<T> list, V target) 

Method Source Code

//package com.java2s;
//License from project: Creative Commons License 

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**/*w w w  .  jav  a2  s.c o  m*/
     * Creates a new list of the objects cast as the type passed as target.
     * <b>WARNING</b>: BESURE YOU KNOW THIS WILL WORK.
     * 
     * @param list
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T, V> List<V> castListElem(List<T> list, V target) {
        List<V> result = new ArrayList<V>(list.size());
        for (T elem : list) {
            result.add((V) elem);
        }
        return result;
    }
}

Related

  1. castList(Class klass, List list)
  2. castList(Collection c)
  3. castList(final List original)
  4. castList(final Object object)
  5. castList(List list, Class clazz)
  6. castListUnchecked(Object list)
  7. castNonNullListParameterTo(String parameterName, List value, Class requiredType)
  8. castOrCopyToList(Iterable iterable)
  9. castTo(final List list, final Class clasz)