I was messing around with some hashCode + equal + map materials and found something ... weird.
The passage is as follows:
class Obj { String n; Obj(String n) {this.n = n;} public int hashCode() {return 0;} public boolean equals(Object o) {return false;}
Then I did something like this:
java.util.Map<Obj,String> map = new java.util.HashMap<Obj,String>(); Obj o1 = new Obj("1"); Obj o11 = new Obj("1"); Obj o2 = new Obj("2"); map.put(o1,"val 1"); map.put(o11,"val 2"); map.put(o2,"val 3"); p("size = " + map.size());
The last line is the weird part. The last line returns val 1 . How so? The equals method always returns false . Is this because the == operator is used before equals is called?
Thank you for understanding.
aMiGo
source share