Hi,
I tried the same code, its working fine on my pc.
But I have another question regarding this.
With reference to line#5, it states in the book that(taken from pg 564 bottom para):
Why didn't we find the Cat key String? Why did it work to use an instance of Dog as a key, when using an instance of Cat as a key failed?
It's easy to see that Dog overrode equals() and hashCode() while Cat didn't.
1) I tried commenting the equals() and hashCode() used in Dog class, but the code runs and produces the same output.
2) If this code set is used:
instead of this one given in the book:
it produces the expected output (Cat key).
So, I feel with regards to 1) and 2), its really not a MUST for a class to override the equals() and hashCode() for using as apart of the key.
Please comment and explain why this behaviour?