Here you can find the source of castList(List
Parameter | Description |
---|---|
list | List to be recast. |
clazz | Class to which to cast list elements. |
@SuppressWarnings("unchecked") public static <T, E extends T> List<E> castList(List<T> list, Class<E> clazz)
//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; } }