In the first call to get(), the hashcode is 8 (magnolia) and it should be 6 (clover), so the retrieval fails at step 1 and we get null. In the second call to get(), the hashcodes are both 6, so step 1 succeeds. Once in the correct bucket (the "length of name = 6" bucket), the equals() method is invoked, and since Dog's equals() method compares names, equals() succeeds, and the output is Dog key. In the third invocation of get(), the hashcode test succeeds, but the equals() test fails because arthur is NOT equal to clover.
======================= I think the bold line above should be, System.out.println(m.get(d1); // #3
Not sure if I got this wrong. Please help and let me know if it is misprint or I do not understand the concept.
It's not a misprint, the information and the example are correct.
The point to be proved is that despite the fact that hashcode of two Dog instances having names "clover" and "arthur" are the same, the objects themselves aren't because the equals method compares their names as Strings. So, while the bucket in the hashmap where the object is found is the same for the Dog instances with names "clover" and "arthur", the object that we are looking for in line 3, a Dog instance with name "clover" is not present in the bucket.