Help coderanch get a
new server
by contributing to the fundraiser
  • 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Regarding HashSet and LinkedHashSet

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The following sentences are from SCJP K&B book, page no:543

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.

I would be very thankful,if any one explains the concept in above sentence with detail example.

I wrote the program to understand above concept. The code is as follows:



Can I assume that, because I didn't override the hashCode() of Object class in Employee class, LinkedHashSet is allowing duplicates.

Please correct if I am wrongly understood the above concept?

Thanks in advance?

Have a wonderful day ahead!

Regards,
Gopal
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can I assume that, because I didn't override the hashCode() of Object class in Employee class, LinkedHashSet is allowing duplicates.


Well it depends on what you define as duplicates. Fot the HashSet both your Employee instances Vasu are completely different objects. If you don't want these objects to be treated differently you have to override the java.lang.Object's default equals() implementation (and don't forget to override hashCode() as well!).
[ October 15, 2008: Message edited by: Thomas Thevis ]
 
Ranch Hand
Posts: 220
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you understood it true ! you must override both hashcode and equals method

Because Java cannot know anything about your user defined objects so you should present the equality for them. So, Sets cannot know anything your object than it allowes dublicates of object in it. Normally for wrappers (because they override equals,hashcode well) they won't add in Set with dublicates so you must override and tell the compiler what is the rule of equality otherwise your Set won't work as you expect


 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's not because you didn't override the hashCode() of Object class in Employee class,but the equals() method
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sharing my current thinking here. If it is wrong, please tell me so I can correct my thinking.

The way it looks to me, Hash based collections store things by using "hash buckets", the different hash values to detect difference on a coarse scale.

Hashing is implemented by having a certain number of hash "buckets" in which to collect objects. When you add a new element to a HashSet, the first thing add() looks at is the hashCode(). If it is different from all others stored there then there is no need to look at the equals() method at all. Since the hashCode is unique then it can't possibly be equal to anything currently IN the container. However, if the hashCode matches THEN add() calls the equals() method. If it returns false, then add() goes ahead and adds the element because it IS unique. If equals() returns true, then add() does NOT add this element becuase it's "equal" is already in the list.

With the above in mind, if you do not override hashCode() then the hashCode() that is inherited from Object will always be unique. No two objects will ever have an equal hashCode so the container will always add the new element. It won't even look at the equals() method.

Man.....I hope this makes sense....
 
Thomas Thevis
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Bob,

good explanation. I think you're right.

Regards,
Thomas
 
V. Potluri
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bob,

Thanks for clearing my doubt. Thanks for your detailed explanation.
Have a wonderful day ahead!

Regards,
Goapl
 
reply
    Bookmark Topic Watch Topic
  • New Topic