Java: How to check if a Field is of type java.util.Collection

37,715

Solution 1

if (Collection.class.isAssignableFrom(field.getType())) {

}

Solution 2

You should use Class.isAssignableFrom:

if (Collection.class.isAssignableFrom(field.getType())
    ...

Solution 3

Using the getType() method

Field field =  ...;
if ( Collection.class.isAssignableFrom( field.getType() ) ){
  //do something with your collection
}

Solution 4

//This execute if

 List<String> cashType = split(" ,AlL ");
 if(cashType instanceof Collection){
     System.out.println("cashType instanceof Collection");
 }else{
     System.out.println("cashType is not instanceof Collection");
 }

//This executes else

List<String> cashType = split(" ,AlL ");
 if(cashType instanceof Hashtable){
     System.out.println("cashType instanceof Collection");
 }else{
     System.out.println("cashType is not instanceof Collection");
 }
Share:
37,715

Related videos on Youtube

Quantum_Entanglement
Author by

Quantum_Entanglement

Updated on December 08, 2020

Comments

  • Quantum_Entanglement
    Quantum_Entanglement over 3 years

    I have a utility method that goes through various classes and recursively retrieves the fields. I want to check if that field is a Collection. Here is some sample code:

    void myMethod(Class<?> classToCheck)
    
    Field[] fields = classToCheck.getDeclaredFields();
    
    for(Field field:fields)
    {
       // check if field if a Collection<?>
    
    }
    

    Thanks in advance for the help.

    • DJClayworth
      DJClayworth over 12 years
      Do you want to test whether the declared type of the field is an implementation of Collection, or whether the actual object referred to by the field in any given object implements Collection? The two results would differ if a) the Field was declared as Object, but an object implementing Collection was assigned to it, or b) the Field was declared as Collection (or a descendant) but it was null.
    • Dave Jarvis
      Dave Jarvis over 9 years
  • Bozho
    Bozho over 12 years
    nope, that's not it. This will get the class that owns the field, rather than the field type
  • Robin
    Robin over 12 years
    Indeed. I just noticed my mistake by actually reading the javadoc. So far relying on my memory. Corrected my answer
  • DJClayworth
    DJClayworth over 12 years
    isAssignableFrom takes a Class, where getType() returns a Type
  • Bozho
    Bozho over 12 years
    @DJClayworth nope, it returns Class<?>. Check the javadoc
  • Kirk Woll
    Kirk Woll over 12 years
  • Trevor Freeman
    Trevor Freeman over 12 years
    @user979467 I just tested it, it works. Maybe you are doing something else wrong?
  • DJClayworth
    DJClayworth over 12 years
    @Bozho My apologies, I found documentation that said it returned Type. Don't know where I found it now, so I'll just withdraw the comment.
  • DJClayworth
    DJClayworth over 12 years
    Then it isn't an instanceof Collection. instanceof handles this case correctly, I believe.
  • DJClayworth
    DJClayworth over 12 years
    New edits make it clear this wasn't the question the OP was asking, but I'll leave this here as a reference.
  • Radhesh Khanna
    Radhesh Khanna over 3 years
    Make sure you use the Correct Import for Collection. Shouldn't be from the org.hibernate package by mistake.
  • user1079475
    user1079475 over 2 years
    And how do you actually iterate over that collection?