File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in kathy sierra about hashcode method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in kathy sierra about hashcode method" Watch "Doubt in kathy sierra about hashcode method" New topic
Author

Doubt in kathy sierra about hashcode method

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
Hi,

I read this in kathy sierra book:


When using HashSet or LinkedHashSet, the objects you add to them
must override hashCode(). If they don’t override hashCode(), the default Object.
hashCode() method will allow multiple objects that you might consider "meaningfully
equal" to be added to your "no duplicates allowed" set.

But what i understand that if you override equals but do not override hashcode, then all the objects will land in the same bucket which will take more memory space and higher time to find the object, but why will it land objects that are meaningfully equal coz they are nowhere related to equals method.
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

Jacob Sonia wrote:Hi,

I read this in kathy sierra book:


When using HashSet or LinkedHashSet, the objects you add to them
must override hashCode(). If they don’t override hashCode(), the default Object.
hashCode() method will allow multiple objects that you might consider "meaningfully
equal" to be added to your "no duplicates allowed" set.

But what i understand that if you override equals but do not override hashcode, then all the objects will land in the same bucket which will take more memory space and higher time to find the object, but why will it land objects that are meaningfully equal coz they are nowhere related to equals method.


If you override equals() and do NOT override hashCode(), the hashCode() method of Object class will be called, and it will assign different hashCode even if the objects are meaningfully equal.

For example if you have a Dog{} class and you override equals() and do NOT override hashCode() and you added two Dog() objects to a HashSet, two objects will be having different hashCode(), even if the equals() return true! So both Dog() objects are considered NOT duplicate and the size() returns 2.


Consider the following code which DOES NOT override hashCode()




The output will be something like
  • Dog name = Fluffy hashCode = 11394033
  • Dog name = Fluffy hashCode = 1671711


  • But if you decide LATER to override hashCode(), see the code below:

    The output will be:
    Dog name = Fluffy hashCode = 2107367690
    (there is only ONE "Fluffy"

    hope this is clear

    SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
    Jacob Sonia
    Ranch Hand

    Joined: Jun 28, 2009
    Posts: 174
    thanks a lot for the nice reply, so it boils down to that when adding elements in a hashset, it is not only equals which is checked while comparing things but also hashcode. Am i right?
    Rajeev Rnair
    Ranch Hand

    Joined: Mar 22, 2010
    Posts: 308

    Jacob Sonia wrote:thanks a lot for the nice reply, so it boils down to that when adding elements in a hashset, it is not only equals which is checked while comparing things but also hashcode. Am i right?


    exactly!

    hasCode() will assign elements in different buckets based on hashCode().
    Even if hashCode() is same, equals() could be different! In this case objects with same hashCode() and different equals() could go to same bucket!

    For retrieving objects from a Collection, it will use both hashCode() and equals()
    hashCode() will be used to find the correct bucket, and equals() will be used to find the right object in the correct bucket.

    Same logic applies to HashMap<E>, LinkedHashMap<E>, LinkedHashSet<E> etc.

    If you use TreeSet<E> (or TreeMap<E>) the above code will give ClassCastException because Dog() objects are NOT mutually comparable. You have to implement the Comparable<E> interface in the Dog{} class!

    good luck!

    Bhaarat Sharma
    Ranch Hand

    Joined: Jun 04, 2007
    Posts: 96
    As you will also find out while doing the questions from this chapter that if you override equals() but do NOT override hashCode() then meaningfully the XXSet will allow you to have duplicates. Because remember for two objects to be equal.....the equals() method has to be true AND also hashCode().


    Sample code provided by Rajeev should clear all your doubts


    omnipresent
    Prithvi Sehgal
    Ranch Hand

    Joined: Oct 13, 2009
    Posts: 774
    Hello,

    Always remember that object retrieval from such collections is always a two step procedure.

    1- First hashCode() will be calculated to find out which bucket the object has landed.

    2- Once a correct bucket is found, then equals method is called on all objects in that bucket. If a match is found then rest objects
    will not be compared.

    Hopet his helps,


    Prithvi,
    My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
    Prashanth Patha
    Ranch Hand

    Joined: May 06, 2011
    Posts: 62

    Thanks for such a good explanation....

    Thanks & Regards
    Prashanth

     
    Consider Paul's rocket mass heater.
     
    subject: Doubt in kathy sierra about hashcode method
     
    Similar Threads
    K&B - Erratum ?
    K&B doubt exam watch ch 7 pg 543
    Help - Exam Watch about Collection -K&B
    About the advantages of overriding equals()
    S&B hashCode() clarification needed.