The following map's first value should be overriden by third value but is is not. why?
import java.util.*;
class A
{
int a, b;
A(int a,int b) {
this.a = a;
this.b = b;
}
public boolean equals(A aa) {
if(this.a == aa.a && this.b == aa.b) {
return true;
}
return false;
}
public int hashCode() {
return this.a-this.b;
}
}
main class
public class MyClass { // main class
public static void main(String args[]) { // main method
Map<A,Character> map = new LinkedHashMap<>();
map.put(new A(1,2),'a'); //first
map.put(new A(1,3),'b'); //second
map.put(new A(1,2),'v'); //third
for(A a : map.keySet()) {
System.out.println(a.a+" "+a.b+" "+map.get(a));
}
}
}
the output by the code is :
1 2 a
1 3 b
1 2 v