java 为什么 ArrayList.contains(Object.class) 不能用于查找实例类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16681786/
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
Why doesn't ArrayList.contains(Object.class) work for finding instances types?
提问by x4nd3r
Say I have an ArrayList which is populated with objects of different types...
假设我有一个 ArrayList,它填充了不同类型的对象......
ArrayList<Fruit> shelf = new ArrayList<Fruit>();
Apple apple = new Apple();
Orange orange = new Orange();
Pear pear = new Pear();
shelf.add(apple);
shelf.add(orange);
shelf.add(pear);
I want to find out if shelf
contains an Orange
object. I've tried
我想知道是否shelf
包含一个Orange
对象。我试过了
shelf.contains(Orange.class)
but this doesn't return true. My understanding is that contains
makes use of the equals
method for object comparison, so I'm not sure why this is the case.
但这不会返回true。我的理解是contains
使用equals
对象比较的方法,所以我不确定为什么会这样。
I realise I can simply iterate through the ArrayList and check the type of the objects individually, but I'm curious as to why contains
doesn't behave the way I expect it to.
我意识到我可以简单地遍历 ArrayList 并单独检查对象的类型,但我很好奇为什么contains
不按照我期望的方式运行。
回答by FThompson
You are correct, contains
uses equals
. However, an instance of a class is not equal to an object of the class, i.e. Orange.class.equals(new Orange())
is false.
你是对的,contains
使用equals
. 但是,类的实例不等于类的对象,即为Orange.class.equals(new Orange())
假。
You will need a custom method to check for a list containing an instance of a class.
您将需要一个自定义方法来检查包含类实例的列表。
public static <E> boolean containsInstance(List<E> list, Class<? extends E> clazz) {
for (E e : list) {
if (clazz.isInstance(e)) {
return true;
}
}
return false;
}
And here's a Java 8 version making use of the Stream API and lambdas:
这是一个使用 Stream API 和 lambdas 的 Java 8 版本:
public static <E> boolean containsInstance(List<E> list, Class<? extends E> clazz) {
return list.stream().anyMatch(e -> clazz.isInstance(e));
}
回答by 0x6C38
Try this:
试试这个:
boolean containsObjectOfType(Object o){
for (int i=0;i<shelf.getSize();i++){
if (shelf.get(i).getClass().equals(o.getClass())){
return true;
}
}
return false;
}
回答by Craig
From the Javadoc for ArrayList#contains
来自ArrayList#contains的 Javadoc
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
如果此列表包含指定的元素,则返回 true。更正式地说,当且仅当此列表包含至少一个元素 e 使得 (o==null ? e==null : o.equals(e)) 时才返回 true。
You need to provide the contains
method with an instance of the class, not the class object.
您需要为contains
方法提供类的实例,而不是类对象。
ArrayList<Fruit> shelf = new ArrayList<Fruit>();
Apple apple = new Apple();
Orange orange = new Orange();
Pear pear = new Pear();
shelf.add(apple);
shelf.add(orange);
shelf.add(pear);
shelf.contains(apple); // returns true