• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt Regarding HashSet

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

Please have a look at the code:



The output is :

Adding: Hans
Adding: Lotte
Adding: Jane
Adding: Hans
Adding: Jane

HashSet does not allow duplicates. then why is it printing them?
Please explain

Regards
Mansukhdeep
 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't override hashcode and equals method.
 
Vijaypal Singh
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my way of understanding it,
Unless you override the equals() and hashcode() of the Person Calss the set will contain unique objects of Persona class.

So if you print g the output will be like "Lotte, Jane, Hans, Jane, Hans"
but if you override the equals and hashcode of Person class,It will understand that unique names are required.

@Override
public boolean equals(Object arg0) {
Person thisPerson = (Person)arg0;
return thisPerson.name.equals(this.name);
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.name.hashCode();
}

so the output becomes like "Jane, Lotte, Hans"

With reference to the print theyare appearing becaouse that gets executed before the call the HashSet;the difference is visible if you place a print after the addition.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in this case, even if you did override equals() and hashCode() in the Person class correctly, you would still get the same output.

The program doesn't show what's in the map, it only shows what you're putting in. You can add two objects that are equal twice into a Set; you will not get an exception or anything like that when you do that. What will happen is that the second object will replace the first object that was already in the map.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:The program doesn't show what's in the map, it only shows what you're putting in.

@OP : Important point!
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:But in this case, even if you did override equals() and hashCode() in the Person class correctly, you would still get the same output.

The program doesn't show what's in the map, it only shows what you're putting in. You can add two objects that are equal twice into a Set; you will not get an exception or anything like that when you do that. What will happen is that the second object will replace the first object that was already in the map.


Hi Jesper

I ran the following code to check what are the contents of the set:



I got the output :
Jane
Lotte
Jane
Hans
Hans

Your claim was that if I try inserting duplicate elements, the second one would replace the first one. But here, both "Jane" and "Hans" are present twice over. Please explain.

Regards
Mansukhdeep
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naresh Shanmugam wrote:You didn't override hashcode and equals method.

But keep in mind that hashCode is with a capital C and equals should take an Object, not a Person.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:Hi Jesper

I ran the following code to check what are the contents of the set:

...

I got the output :
Jane
Lotte
Jane
Hans
Hans

Your claim was that if I try inserting duplicate elements, the second one would replace the first one. But here, both "Jane" and "Hans" are present twice over. Please explain.

Regards
Mansukhdeep

Aha, but that code is slightly different from what you originally posted above. Now, you're printing what's in the set, instead of printing what you're putting into the set.

You don't have duplicate elements, because you haven't defined what "duplicate" means for a Person object. Person objects are not automatically equal if the "name" property of the two objects is the same.

As already said, you must implement a hashCode() and equals() method in class Person to define what equality means for Person objects.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how the hashset detect that the object,value ,string,integer whatever is going to add into it..........is a duplicate........already exist.............
by implements hashcode and equals methods..............

the implementation of both is necessary..............because equals methods will check whether two objects are equals or not....
it will be able to determine if those objects are put in a same hash bucket............

you can override only a equals method but that even doesnot work because only the combination of both will determine the duplicate value...


Remember String,integer,float,long implicitly implemented a hashcode and equals methods................



 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shanky sohar wrote:how the hashset detect that the object,value ,string,integer whatever is going to add into it..........is a duplicate........already exist.............
by implements hashcode and equals methods..............

the implementation of both is necessary..............because equals methods will check whether two objects are equals or not....
it will be able to determine if those objects are put in a same hash bucket............

you can override only a equals method but that even doesnot work because only the combination of both will determine the duplicate value...


Remember String,integer,float,long implicitly implemented a hashcode and equals methods................





I wish I could find out which language starts (most) sentences with a small letter and ends sentences with multiple full stops. It just makes it very very difficult for people like me who expect a capital letter to start a sentence and a single full stop (or exclamation or question mark) to end a sentence.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sabre wrote:
shanky sohar wrote:how the hashset detect that the object,value ,string,integer whatever is going to add into it..........is a duplicate........already exist.............
by implements hashcode and equals methods..............

the implementation of both is necessary..............because equals methods will check whether two objects are equals or not....
it will be able to determine if those objects are put in a same hash bucket............

you can override only a equals method but that even doesnot work because only the combination of both will determine the duplicate value...


Remember String,integer,float,long implicitly implemented a hashcode and equals methods................





I wish I could find out which language starts (most) sentences with a small letter and ends sentences with multiple full stops. It just makes it very very difficult for people like me who expect a capital letter to start a sentence and a single full stop (or exclamation or question mark) to end a sentence.


Next time i will be careful about it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic