By using java reflection, we can easily know if an object is an array. What's the easiest way to tell if an object is a collection(Set,List,Map,Vector...)?
6 Answers
if (x instanceof Collection<?>){
}
if (x instanceof Map<?,?>){
}

- 257,207
- 101
- 511
- 656
-
7Note the `?` in there. You cannot get to the erased component type. If you need to know what kind of objects could be in the collection, you need to look at the elements themselves. – Thilo Jan 12 '16 at 01:20
-
3How to find out what kind of objects are there in the collection when there are no elements? – MichalH Jul 04 '16 at 19:05
Update: there are two possible scenarios here:
You are determining if an object is a collection;
You are determining if a class is a collection.
The solutions are slightly different but the principles are the same. You also need to define what exactly constitutes a "collection". Implementing either Collection
or Map
will cover the Java Collections.
Solution 1:
public static boolean isCollection(Object ob) {
return ob instanceof Collection || ob instanceof Map;
}
Solution 2:
public static boolean isClassCollection(Class c) {
return Collection.class.isAssignableFrom(c) || Map.class.isAssignableFrom(c);
}
(1) can also be implemented in terms of (2):
public static boolean isCollection(Object ob) {
return ob != null && isClassCollection(ob.getClass());
}
I don't think the efficiency of either method will be greatly different from the other.

- 616,129
- 168
- 910
- 942
-
-
1
-
I believe instanceof is more efficient than isAssignableFrom, isn't it? – duduamar Apr 16 '10 at 08:53
-
I'd use reflection (isAssignableFrom) only when necessary, i.e. the class to test against is not known at compile-time. Otherwise, a simple instanceof is easier to type and read, imho. – Thilo Apr 16 '10 at 08:55
-
@duduamar probably you are right (should look at the bytecodes). For sure the usage of isAssignableFrom can't outperform instanceof – Andrea Polci Apr 16 '10 at 08:56
-
I perhaps misinterpreted the question in that I was thinking you had a class (even though the question says "object"). I've since updated to reflect the two scenarios. – cletus Apr 16 '10 at 08:59
-
@Andrea Polci: I use instanceof too. I like this answer because it is another point of view. Before i don't know about using reflection in such way. To know smth new is a pleasant for me. I wrote "the task can be sovled", the main word is CAN. You need be more attentive when you type my name. – den bardadym Apr 16 '10 at 15:56
-
@den baradym ok, put this way I am with you. It's important to know a different approach (may be the only one in different context, like @cletus clarified later) – Andrea Polci Apr 18 '10 at 10:08
-
@Andrea Polci: Thanks. Please use Copy-Paste when write my sername) You wrote :@den bardmadym, @den baradym, what is next? XD. My name is dennis bardadym. – den bardadym Apr 18 '10 at 12:15
Since you mentioned reflection in your question;
boolean isArray = myArray.getClass().isArray();
boolean isCollection = Collection.class.isAssignableFrom(myList.getClass());
boolean isMap = Map.class.isAssignableFrom(myMap.getClass());

- 1,465
- 1
- 14
- 10
-
1
-
Also you can use it, you'll get NPE if a `myArray` reference is null. So need to check for null is required as a result. With checking via `instanceof` it is not necessary. See https://stackoverflow.com/questions/2950319/is-null-check-needed-before-calling-instanceof – Alexandr Nov 05 '20 at 09:54
-
@Alexandr: None of the other answers have an `else` clause or a check for `null`. At least with this answer you are forced to consider the null option. – Foumpie Nov 09 '20 at 14:02
-
@Foumpie, `obj instanceof Collection`. I never get a NPE. I don't need to check for null. The result of this statement will be false, in case `obj` is null. Via reflection, you get NPE without explicit check. – Alexandr Nov 10 '20 at 05:45
Java conveniently has the instanceof
operator (JLS 15.20.2) to test if a given object is of a given type.
if (x instanceof List<?>) {
List<?> list = (List<?>) x;
// do something with list
} else if (x instanceof Collection<?>) {
Collection<?> col = (Collection<?>) x;
// do something with col
}
One thing should be mentioned here: it's important in these kinds of constructs to check in the right order. You will find that if you had swapped the order of the check in the above snippet, the code will still compile, but it will no longer work. That is the following code doesn't work:
// DOESN'T WORK! Wrong order!
if (x instanceof Collection<?>) {
Collection<?> col = (Collection<?>) x;
// do something with col
} else if (x instanceof List<?>) { // this will never be reached!
List<?> list = (List<?>) x;
// do something with list
}
The problem is that a List<?>
is-a Collection<?>
, so it will pass the first test, and the else
means that it will never reach the second test. You have to test from the most specific to the most general type.

- 376,812
- 128
- 561
- 623
-
1
-
2@Thilo: brainfarted; modified to `List`. Important point still stands. – polygenelubricants Apr 16 '10 at 09:10
Test if the object implements either java.util.Collection
or java.util.Map
. (Map
has to be tested separately because it isn't a sub-interface of Collection
.)

- 133,037
- 18
- 149
- 215
Have you thinked about using instanceof
?
Like, say
if(myObject instanceof Collection) {
Collection myCollection = (Collection) myObject;
Although not that pure OOP style, it is however largely used for so-called "type escalation".

- 22,052
- 14
- 85
- 185
-
1Old developpers like me sometime tend to rely upon the pre-java1.5 notation for non compilable examples like this one, especially when the generics notation don't add any value. – Riduidel Apr 16 '10 at 09:27
-
The value added is that you're explicitly declaring the type is not known. There is no way to convey this information in pre-1.5 notation. – Max Jan 15 '16 at 11:28
-
@Max Acting like a resurector ? i see what points you're searching ;-) Anyway, in Java 5+, `Collection` and `Collection>` are equivalent, as both will contain `Object` instances. As a consequence, there is no added value here for generics. – Riduidel Jan 16 '16 at 14:16