aspose file tools*
The moose likes Beginning Java and the fly likes Doubt Regarding HashSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt Regarding HashSet" Watch "Doubt Regarding HashSet" New topic
Author

Doubt Regarding HashSet

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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


~ Mansukh
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
You didn't override hashcode and equals method.
Vijaypal Singh
Ranch Hand

Joined: May 25, 2004
Posts: 31
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.

---------------------------------<br />S.C.J.P 1.4(86%),S.C.W.C.D 1.4(88%)<br /> <br />God Must love crazy people...<br />He Makes so many of them.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14347
    
  22

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Oct 27, 2005
Posts: 19761
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14347
    
  22

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

Joined: Mar 17, 2010
Posts: 1051

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................




SCJP6.0,My blog Ranchers from Delhi
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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.
 
 
subject: Doubt Regarding HashSet