File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Treeset adding not based on equals method ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Treeset adding not based on equals method ?" Watch "Treeset adding not based on equals method ?" New topic
Author

Treeset adding not based on equals method ?

greg poyer
Greenhorn

Joined: Nov 20, 2011
Posts: 1
Hi all,

I didn't understand the result of this code :



If I look equals method any of the 3 cards are equals but the size result is 1 :/

It seem that adding in Treeset is bases on compareTo, not on equals . Is it true ?

Thanks for advice.
John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
Well, when an element is added to a TreeSet then more than equals() is needed as the question is where to insert the element. This is done by comparing the element to be added to elements already in the set by using compareTo(). In a HashSet all what is needed when adding an element is whether there is an element already in the set which is equal to the element to be added so is is enough to use the equals() method. If you change your TreeSet to a HashSet then the resulting size is 3. Actually you haven't overridden the hashCode() method so when using a HashSet even doing three timeswill result in a size of 3

John
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
Good Day,

What does the @Override annotation add here? The code works just fine without these annotations...

Regards,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

Dan Drillich wrote:
What does the @Override annotation add here? The code works just fine without these annotations...


That annotation is added by tools when they generate code that will override a method of the base class. As a developer, you should add that annotation yourself, if you know that you are overriding a method too.

What that annotation does, is tell the compiler that the method is overriding another method. And if it is *not* overriding another method, then the compiler will complain. This way, if you accidentally, call your equals() method, the equal() method, ie. accidentally left out the "s", the compiler will complain. You see the error, and then fix it..... without the annotation, the equal() method won't generate an error, and you'll spent an hour or so debugging why it doesn't work.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
Thank you Henry.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

The code return size 1 not 3 , my explanation

It because of compareTo() method which returns "0" for all three objects , which in turns means that all three objects which were added are equals hence only one object is added to TreeSet.

Correct me if i am wrong.

my question is what happens to overridden equals() method ?

Regards
naveen






John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
my question is what happens to overridden equals() method ?

It is not used as compareTo() is used instead. equals() tells you whether two objects are equal. compareTo() also tells you whether two objects are equal (returns zero if equal. returns non-zero if not equal). In addition compareTo() tells you how to arrange objects so they are sorted (for example object 1 comes before object 2, object 2 comes after object 3, ...). Therefore compareTo() provides you with additional information. When adding an element to a HashSet then this information is not needed as you only need to know whether or not an element is already in the set. When adding an element to a TreeSet then this information is needed as in addition to whether an element is already in the set you have to know where to put it (if it is not already in). You only use compareTo() when you need the extra information (and then you don't need equals() anymore).

John
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Treeset adding not based on equals method ?