Method to check if a Class is a Collection or not. - Java Reflection

Java examples for Reflection:Class

Description

Method to check if a Class is a Collection or not.

Demo Code


//package com.book2s;

import java.util.*;

public class Main {
    public static void main(String[] argv) {
        Class c = String.class;
        System.out.println(isClassCollection(c));
    }/*from   ww  w .ja  va  2 s. c om*/

    /**
     * Method to check if a Class is a Collection or not.
     * @param c the Class to inspect.
     * @return if true the class extend or implememnt Collection.
     */
    public static boolean isClassCollection(Class<?> c) {
        return Collection.class.isAssignableFrom(c)
                || Map.class.isAssignableFrom(c);
    }
}

Related Tutorials