This might seem a simple question but I’ve been trying for a couple of hours now (without having to implement hashCode comparison) to make containsKey working. To simplify things I’ll post a simple code example that i’m having issues with:
public class myPair {
private int a;
private int b;
myPair(int x, int y) {
a=x;
b=y;
}
public boolean equals(Object pair) {
System.out.println("Ola");
return true;
}
int first() { return a; }
int second() { return b; }
public String toString() {
return "X: "+this.a + " Y:"+this.b;
}
}
public class Main {
public static void main(String args[]){
Map<myPair,String> myMap = new LinkedHashMap<myPair, String>();
myMap.put(new myPair(2, 2), "encontrou me");
if(myMap.containsKey(new myPair(2, 2))){
System.out.println(myMap.get(new myPair(2, 2)));
}
System.out.println(myMap.get(new myPair(2,2)));
}
}
This outputs:
null
I’ve implemented the equals method…why isn’t it working?
Because you must override
hashCodein order to use aHashMap(orLinkedHashMap). That’s just how hash maps work: They first compute the hash code to get a rough idea of where to look for the object. If the hash code does not equal the target objects hash code, it will simply look for the object in the wrong place!This is from the API-documentation of
Object.hashCode:The default implementation of
hashCodewill most of the times not return the same hash code for two distinct object.Basically, you’re violating the contract of the API by overriding
equalsbut nothashCode.