• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Doubt Regarding HashSet

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You didn't override hashcode and equals method.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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................



 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic