I have this simple class
public class Position{
int x;
int y;
Position(int x,int y){...}
public int hashCode(){
Integer ix = this.x;
Integer iy = this.y;
return 13*iy.hashCode() + 43*ix.hashCode();
}
}
Im storing instances in a hashMap, but then can’t retrieve them. I fear its the hashcode implementation. Is there any way to implement it when x and y are not Objects?
That you cannot retrieve them has nothing to do with your hashCode implementation.
As Integer#hashCode just returns its value, you could simplify it as
Are you changing ix and iy after putting the object into the map? That is a big no-no and totally screws up the hashtable.
Also, you need to define
Position#equalsas well.