Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding HashSet and LinkedHashSet

 
V. Potluri
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • 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
 
Thomas Thevis
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • 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 ]
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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


 
long meng
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Hi Bob,

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

Regards,
Goapl
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic