tags:

views:

90

answers:

3

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 myMap=new LinkedHashMap();
        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?

+8  A: 

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.

aioobe
Is there anyway to use a map structure without that?and how could I implement it?
out_sider
You could try another map implementation, such as `TreeMap` (but then your keys needs to be comparable). However, if you override `equals` you're violating the contract of the API if you're *not also* overriding `hashCode`.
aioobe
Ok ill try to implement the hasCode member...thanks...how can I set this issue as solved?
out_sider
Simply click the check-mark to the left of the answer.
aioobe
A: 

you need a hashCode method, and your equals method always returns true, which is incorrect.

hvgotcodes
A: 

You need to also override the hashCode method as explained by aioobe and hvgotcodes.
Since equals is always returning true, the hashCode method must always return the same value:

    @Override
    public int hashCode() {
        return 123;    // qualquer número (any other number)
    }

but I doubt that an object with an equals method always returning true is very useful. Worse as a key for a map: there is no way to have more than one key-value pair in the map since there are no distinct keys. (maybe useful for testing)

Carlos Heuberger