My question is - when I try to use d3 as a key, why do I get null as the value?? Since d3.equals(d2) returns true , and their hashcode are equal -shouldn't the value for d3 be "Dog2" (like it is for d2)?
What am I doing/understanding wrong?
s.get(d3) depends on the HashMap implementation. HashMap "s" does not have a key called "d3" (which will be a reference to some object).
So, it does not know which "value" you want to retrieve. It doesn't know how to resolve "d3" key to get you a value.
I guess that explains why you are getting a null.
reeta, Java is case sensitive. Add the @Override annotation to your overridden methods in the Dog class and you'll spot your mistake in no time.
Darryl Burke wrote:Sorry, Sanjeev, that's not correct. Read the API for HashMap.get(...)
d3 Object is not added to the HashMap Object "s" That's why "null" is getting printed as Sanjeev Says.
Please Correct me if i am wrong or where i went wrong.