| Author |
Regarding HashSet and LinkedHashSet
|
V. Potluri
Ranch Hand
Joined: Mar 29, 2007
Posts: 36
|
|
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
Joined: Sep 02, 2008
Posts: 87
|
|
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 ]
|
SCJP 5.0, SCJD in progress
|
 |
Tuna Töre
Ranch Hand
Joined: Aug 17, 2008
Posts: 219
|
|
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
|
blog: http://tunatore.wordpress.com
SCJP 6.0 + SCWCD 1.5
|
 |
long meng
Ranch Hand
Joined: Oct 10, 2008
Posts: 58
|
|
|
it's not because you didn't override the hashCode() of Object class in Employee class,but the equals() method
|
SCJP 5.0 98%<br />SCWCD 5.0 in progress . . .
|
 |
Bob Ruth
Ranch Hand
Joined: Jun 04, 2007
Posts: 318
|
|
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....
|
------------------------
Bob
SCJP - 86% - June 11, 2009
|
 |
Thomas Thevis
Ranch Hand
Joined: Sep 02, 2008
Posts: 87
|
|
Hey Bob, good explanation. I think you're right. Regards, Thomas
|
 |
V. Potluri
Ranch Hand
Joined: Mar 29, 2007
Posts: 36
|
|
Hi Bob, Thanks for clearing my doubt. Thanks for your detailed explanation. Have a wonderful day ahead! Regards, Goapl
|
 |
 |
|
|
subject: Regarding HashSet and LinkedHashSet
|
|
|