How to check if an object is a collection type in Java?

Using java reflection, we can easily find out if an object is an array. The easiest way to determine if an object is a collection (Set, List, Map, Vector ...)?

+50
java
Apr 16 '10 at 8:40
source share
6 answers
if (x instanceof Collection<?>){ } if (x instanceof Map<?,?>){ } 
+72
Apr 16 '10 at 8:43
source share

Update: There are two possible scenarios:

  • You determine if an object is a collection,

  • You determine if the class is a collection.

The solutions are slightly different, but the principles are the same. You also need to determine what exactly constitutes a “collection”. An implementation of Collection or Map will cover 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 do not think that the effectiveness of any method will be very different from the effectiveness.

+35
Apr 16 '10 at 8:45
source share

Since you mentioned the reflection in your question,

 boolean isArray = myArray.getClass().isArray(); boolean isCollection = Collection.class.isAssignableFrom(myList.getClass()); boolean isMap = Map.class.isAssignableFrom(myMap.getClass()); 
+11
Dec 30 '15 at 12:46
source share

Java conveniently has an instanceof operator ( JLS 15.20.2 ) to check if a given object is 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 is important to check the correct order in these constructions . You will find that if you change the check order in the above snippet, the code will still compile, but it will no longer work . That is, the following code does not 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 else means that it will never reach the second test. You need to test the most specific for the most general type.

+4
Apr 16 '10 at 8:59
source share

Check if the object implements either java.util.Collection or java.util.Map . ( Map needs to be tested separately because it is not a sub-interface of Collection .)

+3
Apr 16 '10 at 8:43
source share

Do you think you are using instanceof ? For example, let's say

 if(myObject instanceof Collection) { Collection myCollection = (Collection) myObject; 

Although this is not the pure style of OOP, it is mainly used for so-called “type escalation”.

+1
Apr 16 '10 at 8:43
source share



All Articles