java - 如何更正 hashCode() 方法以正确使用 HashSet 集合

标签 java hashset hashcode

为了确保 equals 和 hashcode() 得到很好的实现,我们必须确保以下规则

  • 自反性
  • 对称性
  • 传递性
  • 一致性
  • 非无效

但是我的以下实现违反了规则一致性(如果我修改其字段,x将永远不会等于它自己),那么我必须做什么才能使这个测试正确运行?

public class TestHashCode {

public class Point {

    public int x;
    public int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int hashCode() {
        int hash = 3;
        hash = 97 * hash + this.x;
        hash = 97 * hash + this.y;
        return hash;
    }

    public boolean equals(Object obj) {
        // generated code by netbeans IDE
    }

}

@Test
public void testEquals() {
    Point x = new Point(1, 1);
    Set<Point> pointsAsSet = new HashSet<>();
    pointsAsSet.add(x);
    x.x = 3 ;
    Assert.assertTrue(pointsAsSet.contains(x));
}

}

最佳答案

您无法改变 HashSet 成员的属性(参与 equalshashCode 的实现)并期望它能够工作。

要么不要改变这些属性,从 HashSet 中删除该元素在改变它之前,稍后重新添加它:

Point x = new Point(1, 1);
Set<Point> pointsAsSet = new HashSet<>();
pointsAsSet.add(x);
...
pointsAsSet.remove(x);
x.x = 3 ;
pointsAsSet.add(x);
...
Assert.assertTrue(pointsAsSet.contains(x));

作为替代方案,如果您的 Point 中有一些独特的不可变属性类,您可以将其用作 HashMap 中的键(例如 HashMap<Integer,Point> ),然后您将不需要 Point要覆盖的类 equalshashCode .

关于java - 如何更正 hashCode() 方法以正确使用 HashSet 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47811941/

相关文章:

java - 运行 mapreduce 程序时出现 "Java Heap space Out Of Memory Error"

java - JPA Criteria 使用 SUM 和 Group By 查询新列

c# - 使用C#HashSet解决等不等的问题

java - 使用 HashSet 对生日悖论进行蒙特卡罗分析

c# - 关于字符串实习和替代方案

Java 为两个可互换的整数覆盖 equals() 和 hashcode()

java - 循环增量 : Which is "better"?

java - 如何使用 AspectJ 创建可运行的 jar 文件?

Java Arrays.hashcode() 奇怪的行为

java - 如何确保 hashCode() 与 equals() 一致?