0
これを行うにはコーダーパッドを使用していますが、その結果はhashCodeと等価を書き換えた後でも重複しているようです。あなたの助けに感謝!ハッシュコードと同等の書き換えが動作しない、何が間違っている
public static class Point{//just a class
int x, y;
Point(int x, int y){
this.x = x;
this.y = y;
}
// @Override
public int hashCode(){//17 and 31 from effective java
int result = 17;
result = 31 * result + x;
result = 31 * result + y;
return result;
}
// @Override
public boolean equals(Point p){
return p.x == x && p.y == y;
}
}
public static void main(String[] args) {//main func
Set<Point> res = new HashSet<>();
res.add(new Point(100, 0));
res.add(new Point(100, 0));
for (Point a: res){
System.out.println(a.x + " " + a.y);
}
あなたのコードで '// @ Override'アノテーションをコメント解除すると、コンパイラは実際に' equals'を上書きしていないことを伝えます。これは、 'equals'が引数として' Object'をとり、 'Point'をとらないからです。 –
それは '@ Override'を使用する全体のポイントです。 – chrylis