I have the below class which only equals is overridden,
class S {
String txt = null;
S(String i) {
txt = i;
}
public boolean equals(Object o) {
S cc = (S) o;
if (this.txt.equals(cc.txt)) {
return true;
} else {
return false;
}
}
//Default hash code will be called
/*public int hashCode(){
return txt.hashCode();
}*/
}
I add this class objects to a hash map as below,
public static void main(String args[]) {
S s1 = new S("a");
S s2 = new S("b");
S s3 = new S("a");
Map m = new HashMap();
m.put(s1, "v11");
m.put(s2, "v22");
m.put(s3, "v33");
System.out.println(m.size());
System.out.println(m.get(s1));
}
Can explain the result 3 and v11 is printed? Shouldnt the value v11 of s1 is replace by v33 because same equals key is put as s3?