Checks if the list (iterable) contains something of an instance class c - Java Collection Framework

Java examples for Collection Framework:Iterable

Description

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

Demo Code


//package com.java2s;

public class Main {
    /**//from  www .  j av  a  2 s.  c  om
     * 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;
    }
}

Related Tutorials