• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Set

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Classes that implement Set doesnt allow duplicates.

However when I tried addding duplicate elements to those classes , it worked fine. There was no error .

How can I ensure that only unique elements are added to my set ?
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please post your code here for the issue you are talking about?
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Set does not allow duplicates if the elements being entered in the set are implementing the hashcode and equals methods properly.

Before inserting the elements Set first checks the hashcode of the element and then will compare the current element with already existing elements having same hash code. If equals returns true it will not insert the element.




Out put of above code is



It does not allow second Integer to add to set and produce the duplicate because Integer is a wrapper class which implements the hashcode and equals methods.

 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add to what Harpreet has written,

If you are using TreeSet class, then check compareTo() method and see what results it is giving.

TreeSet uses compareTo method to check whether objects are sme or not.

hashcode method doesnt come into picture with TreeSet.

correct me if I am wrong
 
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when I tried addding duplicate elements to those classes , it worked fine. There was no error


When we add duplicate elements ,it will return false instead of giving any exception or error.

How can I ensure that only unique elements are added to my set ?


If you are using java wrapper classes as elements , then no need to worry about it.
If you are using your own classes as elements, then you should make sure you are overriding equals and hashcode method in efficient way.
 
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

jose chiramal wrote:However when I tried addding duplicate elements to those classes , it worked fine. There was no error .

How can I ensure that only unique elements are added to my set ?


Do you understand how a Set determines if two objects are duplicates? It uses the equals() and hashCode() methods of the objects that you put in the Set. If you did not override equals() and hashCode() correctly then it might do something else than you expect.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes thats correct, not only Wrapper classes but also String since it has equals and override methods.

Only Wrapper classes and String have overridden equals and hashcode method correct ? Or are there any other ?

Also, what do these equals and hashcode methods actually do, since its only because of these two methods that the output is not showing any duplicates in HashSet. I have understood the impact of using/not using equals and hashcode, but not able to understand what these methods actually do.

Please assist.
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Also, what do these equals and hashcode methods actually do, since its only because of these two methods that the output is not showing any duplicates in HashSet. I have understood the impact of using/not using equals and hashcode, but not able to understand what these methods actually do



What you are not able to understand about these two methods.?

hashcode method gives an identifier of the object which is used to locate a place for the object.

equals method is used to check the equality of the objects.

If you are not overriding hascode method then default implementation will give different hashcode for each objects

If you are not overriding equals method then two objects can never be equal. We override equals method to check whether objects are meaningfully equal.


 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me make my questions clear :

1. Which classes override the equals and hashcode methods. Is it only String and Wrapper Classes or are there other classes.

2. How does overriding equals() and hashcode() actually help in NOT DISPLAYING the duplicate values ?

Am really trying to understand "whats so special" about the equals and override() methods.
 
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

jose chiramal wrote:1. Which classes override the equals and hashcode methods. Is it only String and Wrapper Classes or are there other classes.


Any class can override the equals() and hashCode() methods. In your own classes, it's up to you whether you override them or not. If you want to know if a specific class in the standard Java library overrides them, then lookup the API documentation for that class.

jose chiramal wrote:2. How does overriding equals() and hashcode() actually help in NOT DISPLAYING the duplicate values ?

Am really trying to understand "whats so special" about the equals and override() methods.


Hash-based collections such as HashSet and HashMap use those methods to compare the objects that you put in those collections.

To answer your question what's so special about these methods: The implementation of those methods determines what "equality" means for those objects.

Java theory and practice: Hashing it out
Overriding the equals and hashCode methods
HashCode and Equals method in Java object – A pragmatic concept
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic