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 hashCode
in order to use a HashMap
(or LinkedHashMap
). 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
:
- If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
The default implementation of hashCode
will 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 equals
but not hashCode
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With