集册 Java实例教程 检查列表(可迭代)是否包含实例类c

检查列表(可迭代)是否包含实例类c

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

375
检查列表(iterable)是否包含实例类c的内容


//package com.nowjava;

/** from 
时代Java公众号 - nowjava.com**/

public class Main {

    /**

     * Checks if the list (iterable) contains something of an instance class c

     * @param iterable a list of things

     * @param c class to check against

     * @param <T> Type of the list

     * @return if the list contains something of instance class c

     */

    public static <T> boolean containsInstance(Iterable<T> iterable, Class c) {

        for (T t : iterable) {

            if (c.isInstance(t)) {

                return true;

            }

        }

        return false;

    }/* 来 自 N o w  J a v a  . c o m*/

}