Tengo una clase de punto personalizada simple de la siguiente manera y me gustaría saber si mi implementación hashCode podría mejorarse o si esto es lo mejor que va a obtener.Java hashCode para una clase de punto
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();
}
}
Cómo estás tratando para mejorarlo? ¿Quieres intentar hacerlo más rápido? – David
desea garantizar la singularidad? ¿velocidad? – Adrian
Me gustaría garantizar ambos :) –