如何检查对象是否是 Java 中的集合类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2651632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if an Object is a Collection Type in Java?
提问by Sawyer
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...)?
通过使用java反射,我们可以很容易地知道一个对象是否是一个数组。判断一个对象是否是一个集合(Set、List、Map、Vector...)的最简单方法是什么?
采纳答案by Thilo
if (x instanceof Collection<?>){
}
if (x instanceof Map<?,?>){
}
回答by Riduidel
Have you thinked about using instanceof
?
Like, say
你有没有想过使用instanceof
?比如,说
if(myObject instanceof Collection) {
Collection myCollection = (Collection) myObject;
Although not that pure OOP style, it is however largely used for so-called "type escalation".
虽然不是那种纯粹的 OOP 风格,但它主要用于所谓的“类型升级”。
回答by Donal Fellows
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
.)
测试对象是否实现java.util.Collection
或java.util.Map
。(Map
必须单独测试,因为它不是 的子接口Collection
。)
回答by cletus
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.
解决方案略有不同,但原理是相同的。您还需要定义“集合”的确切构成。实现Collection
或Map
将涵盖 Java 集合。
Solution 1:
解决方案1:
public static boolean isCollection(Object ob) {
return ob instanceof Collection || ob instanceof Map;
}
Solution 2:
解决方案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):
(1) 也可以按照 (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.
我不认为这两种方法的效率会与另一种有很大不同。
回答by polygenelubricants
Java conveniently has the instanceof
operator (JLS 15.20.2) to test if a given object is of a given type.
Java 方便地使用instanceof
运算符 ( JLS 15.20.2) 来测试给定对象是否属于给定类型。
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.
问题是 a List<?>
is-a Collection<?>
,所以它会通过第一个测试,并且else
意味着它永远不会到达第二个测试。您必须从最具体到最通用的类型进行测试。
回答by Foumpie
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());