Page 1 of 1
About Question com.enthuware.ets.scjp.v6.2.90 :
Posted: Sat Oct 15, 2011 6:42 pm
by ETS User
I thought the book would be the one in the map because Book overrode .equals() to check the isbn number.
Re: About Question com.enthuware.ets.scjp.v6.2.90 :
Posted: Sun Oct 16, 2011 9:50 am
by admin
Remember that, before comparing the objects with equals() method, a map first tries to locate the bucket in which it thinks it will find the object. It locates the bucket using hashcode of the key object passed in get(key) or put(key, object).
Now, if you do not implement the hashcode, object class's hashcode() will be used. So when you put the original Book object, it's hashcode will be different from the book object that you pass in get(book). Thus, it will not find the right bucket and so it will not be able to find the original book object.