I have a class having two methods: getY() and getX() (it's a sort of map).
Y's and X's domain is [-1000.0, + 1000.0], and they're doubles.
I have written a hashCode() method:
@Override
public int hashCode()
{
int hash=(int) (getX()/EPSILON);
hash+= (int) ( (1000.0/EPSILON)*getY() );
return hash;
}
Where EPSILON is the maximum error tolerated.
But the problem is that values are too high and I get an overflow if X=1000.0 and Y=1000.0 .
How to write a hashCode() method that handles the overflow and is still able to return two hash codes for two different objects in every case?
Mapso that you don't get anIndexOutOfBoundsException? If so you can do a mod somewhere in the code to prevent the overflow.(x,y) equal to (x+eps,y)which your current hashcode implementation doesn't guarantee. Actually I'm pretty sure you can't do this and guarantee transitivity, which means your equals/hashcode is already broken beyond repair