为什么这个String.equals()方法总是返回false? [英] Why does this String.equals() method always return false?
问题描述
我有我实现了自定义哈希code()和equals(Object)方法使用Eclipse方法生成的类。类的每个对象都有一个称为String域 MUID
这应该是唯一的,足以确定相等。在覆盖的方法是这样的:
I have a class for which I have implemented a custom hashCode() and equals(Object) method using the Eclipse method generator. Each object of the class has a String field called mUid
which should be unique and is enough to determine equality. The overridden methods look like this:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((mUid == null) ? 0 : mUid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DataSource other = (DataSource) obj;
if (mUid == null) {
if (other.mUid != null)
return false;
} else if (!mUid.equals(other.mUid))
return false;
return true;
}
但由于某些原因,当我比较我的类的两个对象,它始终返回false。我已经通过了code加强与调试,并可以确定MUID字符串相同。但我的等于(对象)
方法始终返回该行
如果{返回false;}(mUid.equals(other.mUid)!)
。
这意味着字符串equals()方法是说字符串不相等。但是,在调试我可以看到,即使他们的哈希codeS都是平等的。
This means the String equals() method is saying the strings are not equal. But, in the debugger I can see that even their hashcodes are equal.
下面是一个调试截图:
任何人都可以请解释发生了什么事?
谢谢你。
Here is a debugging screenshot: Can anyone please explain what is happening? Thanks.
我要指出,我不打电话了equals(...)方法直接,而是来自列表<&数据源GT; list.contains(数据源)
。事实证明,如果我通过步等于(对象)
code,它表明我,它在列表(每个对象比较,即使有返回false匹配)。但似乎在 List.contains()
方法返回正确的值(true或false)反正。不知道这是为什么,但它的作品。我曾在如果(list.contains(数据源))一个额外的分号;
所以我无法正确地看到包含()
是工作的罚款。 感谢您的帮助大家
I should point out that I am not calling the equals(...) method directly, but rather from List<DataSource> list.contains(DataSource)
. It turns out that if I step through the equals(Object)
code, it shows me that it returns false for comparing every object in the list (even if there is a match). But it seems the List.contains()
method returns the right value (true or false) anyway. Don't know why this is, but it works. I had an extra semicolon after the if(list.contains(dataSource));
so I could not rightly see that contains()
was working fine. Thanks for your help everyone.
推荐答案
您的方法应该工作。我只是这个code测试,它工作得很好:
Your method should work. I just tested with this code and it worked fine:
public class DataSource {
private String mUid;
public DataSource(String m) {
mUid = m;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((mUid == null) ? 0 : mUid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DataSource other = (DataSource) obj;
if (mUid == null) {
if (other.mUid != null)
return false;
} else if (!mUid.equals(other.mUid))
return false;
return true;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DataSource ds1 = new DataSource("test");
DataSource ds2 = new DataSource("test");
System.out.println(ds1.equals(ds2));
}
}
控制台输出是真;
当我改变了code到数据源DS2 =新的DataSource(测试1);
它返回假;
这篇关于为什么这个String.equals()方法总是返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!