Java List Cast castList(List list, Class clazz)

Here you can find the source of castList(List list, Class clazz)

Description

Casts a list containing elements of class T to a list containing elements of a subclass E.

License

Mozilla Public License

Parameter

Parameter Description
list List to be recast.
clazz Class to which to cast list elements.

Return

The recast list.

Declaration

@SuppressWarnings("unchecked")
public static <T, E extends T> List<E> castList(List<T> list, Class<E> clazz) 

Method Source Code

//package com.java2s;
/**//from w w w. ja v  a2 s  .co m
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. 
 * If a copy of the MPL was not distributed with this file, You can obtain one at 
 * http://mozilla.org/MPL/2.0/.
 * 
 * This Source Code Form is also subject to the terms of the Health-Related Additional
 * Disclaimer of Warranty and Limitation of Liability available at
 * http://www.carewebframework.org/licensing/disclaimer.
 */

import java.util.List;

public class Main {
    /**
     * Casts a list containing elements of class T to a list containing elements of a subclass E.
     * 
     * @param list List to be recast.
     * @param clazz Class to which to cast list elements.
     * @return The recast list.
     */
    @SuppressWarnings("unchecked")
    public static <T, E extends T> List<E> castList(List<T> list, Class<E> clazz) {
        return (List<E>) list;
    }
}

Related

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