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?