Here you can find the source of asList(Collection> collection, Class
Parameter | Description |
---|---|
T | a parameter |
collection | a parameter |
type | a parameter |
public static <T> List<T> asList(Collection<?> collection, Class<T> type)
//package com.java2s; /*/*from w ww . j ava 2s. c om*/ * Copyright (c) Ludger Solbach. All rights reserved. * The use and distribution terms for this software are covered by the * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) * which can be found in the file license.txt at the root of this distribution. * By using this software in any fashion, you are agreeing to be bound by * the terms of this license. * You must not remove this notice, or any other, from this software. */ import java.util.ArrayList; import java.util.Collection; import java.util.List; public class Main { /** * Convert the collection to a typed list of given type. * @param <T> * @param collection * @param type * @return */ public static <T> List<T> asList(Collection<?> collection, Class<T> type) { List<T> list = new ArrayList<T>(); for (Object o : collection) { if (type.isInstance(o)) { list.add(type.cast(o)); } } return list; } }