This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding HashSet and LinkedHashSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding HashSet and LinkedHashSet" Watch "Regarding HashSet and LinkedHashSet" New topic
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: 320
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regarding HashSet and LinkedHashSet
 
Similar Threads
Hashset & overriding hashcode()
overriding hashcode and equals method internal work flow
equal( ) and hashCode( ) Examples
S&B hashCode() clarification needed.
A question About overriding equals and hashcode for an Object