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 方法中总会有一个字符串.还有一些 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 - 所以我在这里"消息永远不会出现.我一直在尝试一些不同的方法来处理我的代码,但这是因为这样做的想法是从字符串文本中获取单个字符并检查单个字符是否已经存在于 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.
推荐答案
您的 equals
实现不正确.equals
有特定的契约;该代码试图违反该合同.来自文档:
Your equals
implementation is incorrect. equals
has a specific contract; that code attempts to violate that contract. From the documentation:
equals 方法在非空对象引用上实现等价关系:
The equals method implements an equivalence relation on non-null object references:
- 它是自反:对于任何非<code>null引用值
x
,x.equals(x)
应该返回true
. - 它是对称:对于任何非<code>null引用值
x
和y
,x.equals(y)
应该返回true
当且仅当y.equals(x)
返回true
. - 它是传递:对于任何非<code>null引用值
x
、y
和z
,如果x.equals(y)
返回true
并且y.equals(z)
返回true
,那么x.equals(z)
应该返回true
. - 一致:对于任何非<code>null引用值
x
和y
,多次调用x.equals(y)
始终返回true
或始终返回false
,前提是在equals
比较中没有使用信息对象被修改. - 对于任何非<code>null 引用值
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屋!