*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics Question" Watch "Generics Question" New topic
Author

Generics Question

Tayitu Betule
Ranch Hand

Joined: Dec 18, 2009
Posts: 39
Here is a question taken from K&B.



The output is as follows:
Adding: Hans
Super size: 1
Adding: Lotte
Super size: 2
Adding: Jane
Super size: 3
Adding: Hans
Super size: 4
Adding: Jane
Super size: 5
Total: 5

My question is why is HashSet<Person> adding duplicates at line 16? I thought it should add only the first three. Please explain.

Thanks a lot!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

It's not a Generics question, but a Collection question If you look at the API for the add method of the HashSet class, what does it say ?


[My Blog]
All roads lead to JavaRanch
Tayitu Betule
Ranch Hand

Joined: Dec 18, 2009
Posts: 39
This is what it says:

"Adds the specified element to this set if it is not already present."
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Tayitu Betule wrote:This is what it says:

"Adds the specified element to this set if it is not already present."


Click on the "add" method link. You'll see more details.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
but you're not adding Strings, you're adding Persons. What method does the HashSet use to determine if an object is a duplicate? Does Person override this method?
Tayitu Betule
Ranch Hand

Joined: Dec 18, 2009
Posts: 39
So is it because the Person class doesn't override equal and hashcode method that there is no way to know if duplicates are being added to HashSet<Person> collection? Is my understanding right?

Thanks for all of you.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Tayitu Betule wrote:So is it because the Person class doesn't override equal and hashcode method that there is no way to know if duplicates are being added to HashSet<Person> collection? Is my understanding right?


Since your class doesn't override the hashCode method, it uses the one provided by Object, and so two instructive things would be to 1) read the API regarding the hashCode method for Object and 2) changing one line of your code from this:



to this:
Tayitu Betule
Ranch Hand

Joined: Dec 18, 2009
Posts: 39
The call to o.hashcode() gives distinct int values. I got it now... thank you for all your replies and references.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello,

For your own classes to be used inside the Collection in case of Sets, you need to override hashCode
and equals. That's why your class is allowing duplicates.

Hope this helps,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics Question
 
Similar Threads
what is the correct answer?
Question about TreeSet and HashSet
Generics K&B doubt
K&B Self Test Pg 617, Q12- HashSet doubt