Java的contains()方法返回False,即使重写的equals()方法返回True。

huangapple go评论80阅读模式
英文:

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 &lt; this.columnsOfData.size();i++) {
		System.out.println( ((Record) obj).columnsOfData.get(i).name + &quot; OBJECT &quot; + ((Record) obj).columnsOfData.get(i).value );
		System.out.println( columnsOfData.get(i).name + &quot; THIS &quot; + 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 &amp;&amp; check);
}

I initialize HashSet as follows:

Set&lt;Record&gt; answer = new HashSet&lt;Record&gt;()

and start testing

        Record r1 = new Record();
		r1.columnsOfData.add(new Data(new Double( 1.5 ),&quot;gpa&quot;));
		r1.columnsOfData.add(new Data(new String(&quot;John&quot;),&quot;name&quot;));
		r1.columnsOfData.add(new Data(new Integer( 2 ),&quot;id&quot;));
		
		Record r2 = new Record();
		r2.columnsOfData.add(new Data(new Double( 1.5 ),&quot;gpa&quot;));
		r2.columnsOfData.add(new Data(new String(&quot;John&quot;),&quot;name&quot;));
		r2.columnsOfData.add(new Data(new Integer( 2 ),&quot;id&quot;));

        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 hashCodes 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

huangapple
  • 本文由 发表于 2020年4月7日 02:54:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61066964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定