This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Set" Watch "Set" New topic
Author

Set

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
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 ?
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Can you please post your code here for the issue you are talking about?
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

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

Joined: Apr 29, 2008
Posts: 234
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
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

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.


A.A.Anbarasu
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
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

Joined: Apr 29, 2008
Posts: 234
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

Joined: Feb 12, 2010
Posts: 266
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Set