集册 Java实例教程 获取可迭代的Class c的实例

获取可迭代的Class c的实例

欢马劈雪     最近更新时间:2020-01-02 10:19:05

437
获取iterable中类c的实例


//package com.nowjava;

import java.util.ArrayList;
/*来自 
 时代Java - nowjava.com*/

import java.util.List;


public class Main {

    /**

     * Gets the instances of Class c in iterable

     * @param iterable iterable with instances

     * @param c class of generic type E

     * @param <E> Type of the list to return and the generic of c

     * @return List\<E> where object in list is an instanceof class c of generic type E

     */

    public static <E> List<E> getInstances(Iterable iterable, Class<E> c) {

        ArrayList<E> result = new ArrayList<E>();

        for (Object obj : iterable) {

            
展开阅读全文