Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Treeset ordering

 
Jesse Custer
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to K&B book page 542-543, sets determine if objects are equal by using the equals methods. And a TreeSet orders it's objects according to the compareTo() method.
Now I found out that if two objects are not equal according to equals method() but do have same ordering according to compareTo() they are NOT both inserted in the TreeSet. Not exactly what I was expecting. Can anyone elaborate about this.

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a close read of the API for the Comparable Interface
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
------------------------------------------------------------------
public int compareTo(Item i)
{
return 0;
}
-------------------------------------------------------------------
here is the problem in your code.You have overridden the compareTo(Item i) method but you did not provide the implementation,here you are returning 0 means ,the value in the set is equal.What ever value you put the result is equal.

Replace the code with this

public int compareTo(Item i)

{
if(i.x<this.x)
return -1;
else if(i.x >this.x)
return 1;
else
return 0;
}

you will get the result.

And also check hashCode() method.You are not overridden not overloaded.You defined a new HashCode() method.
And it will not call by equals() method

Thanks
Anil Kumar
[ May 07, 2007: Message edited by: anil kumar ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesse,


I think the culprit lines to your doubt are as following:




Now I found out that if two objects are not equal according to equals method() but do have same ordering according to compareTo() they are NOT both inserted in the TreeSet.


Nay,

You need to see what compareTo returns as Barry pointed out:
Returns
zero if obj1==obj2
positive if obj1>obj2
negative if obj1<obj2

In your definition of the compareTo() you always return 0, means not more than one object can ever be inserted into the TreeSet.

Thanks,
 
Jesse Custer
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry, why didn't i think of doing that . But it helped, thanks.

To Anil and Chandra. Thank you for your replies.
But I was well aware that my compareTo() method had a bad implementation. What I wanted to point out was that the objects would be handled as being equal because of the compareTo() method while they are not according to the equals() method.

It was just something I didn't expect. Imagine a class Person with an id and name. When I have 2 different persons with the same name, i'm unable to put them both in a TreeSet ordered on name, cause they will be considered as not unique.
[ May 07, 2007: Message edited by: Jesse Custer ]
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic