Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Treeset adding not based on equals method ?

 
greg poyer
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Day,

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

Regards,
Dan
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry.
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic