ArrayList的contains()方法始终使用自定义对象返回false [英] ArrayList's contains() method always returns false with custom object
问题描述
我在继续编写代码时遇到了一些麻烦,我将给您提供一个简单的示例(尽管会有些复杂,但这个简单的代码也无法正常工作).
I'm having some troubles with getting on with my code, I'll give you an simple example (though it's going to be a little more complex, this simple code doesn't work properly either).
class Sign {
private String char;
private Integer freq;
public Sign(String c) {
this.char = c;
}
@Override
public boolean equals(Object o) {
String check = (String)o;
return check.equals(this.char);
}
@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + this.char.hashCode();
return hash;
}
}
出于简单的原因,我假设equals方法中总会有一个String.还有一些hashCode()可以确保contains()方法可以正常工作,这是测试本身:
I assume that there's always will be a String in equals method for simplicity reasons. There's some hashCode() also to make sure that the contains() method will work and here's the test itself:
ArrayList<Sign> queueOfSigns = new ArrayList<>();
Sign test = new Sign("C");
String c = "C";
queueOfSigns.add(test);
if(queueOfSigns.contains("C"))
System.out.println("I am here!");
无论如何,在这种情况下,此简单的测试代码始终返回false-这样就永远不会出现我在这里"消息.我一直在尝试一些不同的方法来处理我的代码,但这是因为这样做的想法是从String文本中获取单个字符,并检查ArrayList中是否已经存在单个字符.但是,如果没有使这个简单的测试正常工作,我将无法继续前进,所以我想问你-我想念的是什么.这是我第一次真正使用equals()和hashCode()方法来使自己的对象与contains()方法一起正常工作.
No matter what, this simple test-code always returns false in that case - so "I'm here" message never appears. I've been trying some different ways of approach my code but it was because the idea of this is to get single characters from String text and check whether the single character is already present in the ArrayList. Nevertheless - without getting this simple test working properly I can't move on, so I would like to ask you - what am I missing. It's my first time actually with using equals() and hashCode() methods to get my own object working properly with contains() method.
推荐答案
Your equals
implementation is incorrect. equals
has a specific contract; that code attempts to violate that contract. From the documentation:
equals方法对非null对象引用实现等效关系:
The equals method implements an equivalence relation on non-null object references:
- 它是自反的:对于任何非<c2>参考值
x
,x.equals(x)
应该返回true
. - 它是对称的:对于任何非<c2>参考值
x
和y
,当且仅当y.equals(x)
返回true
时,x.equals(y)
应该返回true
. - 它是 transitive :对于任何非<c2>参考值
x
,y
和z
,如果x.equals(y)
返回true
并且y.equals(z)
返回true
,那么x.equals(z)
应该返回true
. - 这是一致的:对于任何非<c2>参考值
x
和y
,多次调用x.equals(y)
会始终返回true
或一致地返回false
(提供)在对象的equals
比较中使用的信息均未修改. - 对于任何非<c2>参考值
x
,x.equals(null)
应该返回false
.
- It is reflexive: for any non-
null
reference valuex
,x.equals(x)
should returntrue
. - It is symmetric: for any non-
null
reference valuesx
andy
,x.equals(y)
should returntrue
if and only ify.equals(x)
returnstrue
. - It is transitive: for any non-
null
reference valuesx
,y
, andz
, ifx.equals(y)
returnstrue
andy.equals(z)
returnstrue
, thenx.equals(z)
should returntrue
. - It is consistent: for any non-
null
reference valuesx
andy
, multiple invocations ofx.equals(y)
consistently returntrue
or consistently returnfalse
, provided no information used inequals
comparisons on the objects is modified. - For any non-
null
reference valuex
,x.equals(null)
should returnfalse
.
无法将Sign
类equals
的实例创建为字符串.
There's no way to make an instance of your Sign
class equals
to a string.
这篇关于ArrayList的contains()方法始终使用自定义对象返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!