Mam proste niestandardowe klasy Point w następujący sposób i chciałbym wiedzieć, czy moja implementacja hashCode mogłaby zostać ulepszona, czy jest to najlepsze, co dostanie.Java hashCode dla klasy 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();
}
}
Jak starasz poprawić? Czy chcesz spróbować zrobić to szybciej? – David
chcesz zagwarantować wyjątkowość? prędkość? – Adrian
Chciałbym zagwarantować oba :) –