Tôi có một lớp Point tùy chỉnh đơn giản như sau và tôi muốn biết nếu implemention hashCode của tôi có thể được cải thiện hoặc nếu điều này là tốt nhất nó sẽ nhận được.Java hashCode cho một lớp Point
public class Point
{
private final int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
@Override
public boolean equals(Object other)
{
if (this == other)
return true;
if (!(other instanceof Point))
return false;
Point otherPoint = (Point) other;
return otherPoint.x == x && otherPoint.y == y;
}
@Override
public int hashCode()
{
return (Integer.toString(x) + "," + Integer.toString(y)).hashCode();
}
}
Bạn đang cố gắng để cải thiện nó? Bạn có muốn thử nhanh hơn không? – David
bạn muốn đảm bảo tính duy nhất? tốc độ? – Adrian
Tôi muốn đảm bảo cả hai :) –