• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Doubt in kathy sierra about hashcode method

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
     
    Jacob Sonia
    Ranch Hand
    Posts: 185
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 310
    1
    Oracle Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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!

     
    Ranch Hand
    Posts: 96
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    Ranch Hand
    Posts: 774
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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,
     
    Ranch Hand
    Posts: 62
    Eclipse IDE Oracle Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for such a good explanation....
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic