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");
}
Related videos on Youtube
Author by
Quantum_Entanglement
Updated on December 08, 2020Comments
-
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 over 12 yearsDo 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 over 9 yearspossible duplicate of Howto find out if a field is instanceof a type via reflection?
-
-
Bozho over 12 yearsnope, that's not it. This will get the class that owns the field, rather than the field type
-
Robin over 12 yearsIndeed. I just noticed my mistake by actually reading the javadoc. So far relying on my memory. Corrected my answer
-
DJClayworth over 12 yearsisAssignableFrom takes a Class, where getType() returns a Type
-
Bozho over 12 years@DJClayworth nope, it returns
Class<?>
. Check the javadoc -
Kirk Woll over 12 years
-
Trevor Freeman over 12 years@user979467 I just tested it, it works. Maybe you are doing something else wrong?
-
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 over 12 yearsThen it isn't an instanceof Collection. instanceof handles this case correctly, I believe.
-
DJClayworth over 12 yearsNew edits make it clear this wasn't the question the OP was asking, but I'll leave this here as a reference.
-
Radhesh Khanna over 3 yearsMake sure you use the Correct Import for Collection. Shouldn't be from the org.hibernate package by mistake.
-
user1079475 over 2 yearsAnd how do you actually iterate over that collection?