英文:
Java contains() method returns False even though overridden equals() returns True
问题
我有一个名为Record
的类,其中包含一个Data
类对象的向量。
类Data
除了有两个字段之外什么也没有:
Object value;
String name;
我在Record
类中重写了equals方法,如下所示:
@Override
public boolean equals(Object obj) {
boolean check = true;
for (int i = 0; i < this.columnsOfData.size(); i++) {
System.out.println(((Record) obj).columnsOfData.get(i).name + " OBJECT " + ((Record) obj).columnsOfData.get(i).value);
System.out.println(columnsOfData.get(i).name + " THIS " + columnsOfData.get(i).value);
if (!(((((Record) obj).columnsOfData.get(i).name).equals(this.columnsOfData.get(i).name)) || !((((Record) obj).columnsOfData.get(i).value).equals(this.columnsOfData.get(i).value)))) {
check = false;
}
}
return (obj instanceof Record && check);
}
我初始化了一个HashSet,如下所示:
Set<Record> answer = new HashSet<Record>();
并开始测试:
Record r1 = new Record();
r1.columnsOfData.add(new Data(new Double(1.5), "gpa"));
r1.columnsOfData.add(new Data(new String("John"), "name"));
r1.columnsOfData.add(new Data(new Integer(2), "id"));
Record r2 = new Record();
r2.columnsOfData.add(new Data(new Double(1.5), "gpa"));
r2.columnsOfData.add(new Data(new String("John"), "name"));
r2.columnsOfData.add(new Data(new Integer(2), "id"));
System.out.println(r1.equals(r2)); //返回TRUE
answer.add(r1);
System.out.println(answer.contains(r2)); //返回FALSE
如有帮助,请理解问题所在将不胜感激。
英文:
I have an class called Record
that consists of a vector of Data
class objects.
Class Data
has nothing but two fields:
Object value;
String name;
I override the equals method in the Record
class as follows:
public boolean equals(Object obj) {
boolean check = true;
for (int i = 0; i < this.columnsOfData.size();i++) {
System.out.println( ((Record) obj).columnsOfData.get(i).name + " OBJECT " + ((Record) obj).columnsOfData.get(i).value );
System.out.println( columnsOfData.get(i).name + " THIS " + columnsOfData.get(i).value );
if( !((((Record) obj).columnsOfData.get(i).name).equals(this.columnsOfData.get(i).name)) || !((((Record) obj).columnsOfData.get(i).value).equals(this.columnsOfData.get(i).value))) {
check = false;
}
}
return (obj instanceof Record && check);
}
I initialize HashSet as follows:
Set<Record> answer = new HashSet<Record>()
and start testing
Record r1 = new Record();
r1.columnsOfData.add(new Data(new Double( 1.5 ),"gpa"));
r1.columnsOfData.add(new Data(new String("John"),"name"));
r1.columnsOfData.add(new Data(new Integer( 2 ),"id"));
Record r2 = new Record();
r2.columnsOfData.add(new Data(new Double( 1.5 ),"gpa"));
r2.columnsOfData.add(new Data(new String("John"),"name"));
r2.columnsOfData.add(new Data(new Integer( 2 ),"id"));
System.out.println(r1.equals(r2)); //RETURNS TRUE
answer.add(r1);
System.out.println(answer.contains(r2)); //RETURNS FALSE
Any help understanding where the issue is would be greatly appreciated.
答案1
得分: 1
尝试覆盖hashCode方法。应该会起作用。
您可以在这里找到解释:
https://stackoverflow.com/questions/31470515/hashset-contains-method
英文:
Try to override also the hashCode method. It should work.
You can find an explanation here:
https://stackoverflow.com/questions/31470515/hashset-contains-method
答案2
得分: 0
HashSet依赖于这样的约定,即相等对象的hashCode相等。也就是说,如果a.equals(b)返回true,则a.hashCode()必须与b.hashCode()相同。
你应该重写Record的hashCode()方法,以便在HashSet中使用它。
英文:
HashSet relies on the contract that hashCode
s of equal objects are equal. That is, if a.equals(b) returns true, then a.hashCode() must be the same as b.hashCode()
You should override the hashCode() method of Record to use it in a HashSet
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论