Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Question

 
Tayitu Betule
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Tayitu Betule
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what it says:

"Adds the specified element to this set if it is not already present."
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic