Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java map.containsKey doesn't work

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?

like image 250
out_sider Avatar asked Feb 26 '23 00:02

out_sider


1 Answers

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.

like image 127
aioobe Avatar answered Mar 06 '23 18:03

aioobe