我有以下课程:
class Point
{
double x, y;
// .... constructor and other functions here
public boolean equals(Point p)
{
if(p==null) return(false);
return(x==p.x && y==p.y);
}
public int hashCode()
{
int result=17;
long c1=Double.doubleToLongBits(x);
long c2=Double.doubleToLongBits(y);
int ci1=(int)(c1 ^ (c1 >>> 32));
int ci2=(int)(c2 ^ (c2 >>> 32));
result = 31 * result + ci1;
result = 31 * result + ci2;
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我写下面的代码:
Point x=new Point(11,7);
Point y=new Point(11,7);
System.out.println("hash-code of x=" + x.hashCode());
System.out.println("hash-code of y=" + y.hashCode());
System.out.println("x.equals(y) = " + x.equals(y));
System.out.println("x==y = " + (x==y));
java.util.HashSet<Point> s=new java.util.HashSet<Point>();
s.add(x);
System.out.println("Contains "+y.toString()+" = "+s.contains(y));
s.add(y);
System.out.println("Set size: "+s.size());
java.util.Iterator<Point> itr=s.iterator();
while(itr.hasNext()) System.out.println(itr.next().toString());
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
hash-code of x=79052753
hash-code of y=79052753
x.equals(y) = true
x==y = false
Contains (11.0,7.0) = false
Set size: 2
(11.0,7.0)
(11.0,7.0)
Run Code Online (Sandbox Code Playgroud)
请帮助我理解为什么contains()返回false(即使在equals()和hashCode()之后返回相同的值)以及如何纠正这一点(即阻止Java添加重复元素).提前致谢.
您没有覆盖Object中的equals方法.
equals方法的签名是:
public boolean equals(Object obj)
Run Code Online (Sandbox Code Playgroud)
并不是
public boolean equals(Point obj)
Run Code Online (Sandbox Code Playgroud)
请不要使用==比较双值.你应该使用Double.equals.
您已更改方法签名Object.equals(Object),因此您未正确覆盖equals.我建议你使用@Override注释来捕获这类错误.你的方法看起来像,
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o instanceof Point) {
Point p = (Point) o;
return(x==p.x && y==p.y);
}
return false;
}
Run Code Online (Sandbox Code Playgroud)