About Question com.enthuware.ets.scjp.v6.2.90 :
Moderator: admin
About Question com.enthuware.ets.scjp.v6.2.90 :
I thought the book would be the one in the map because Book overrode .equals() to check the isbn number.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.90 :
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.
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.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 116 guests