aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Treeset ordering 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 ordering" Watch "Treeset ordering" New topic
Author

Treeset ordering

Jesse Custer
Ranch Hand

Joined: Feb 07, 2007
Posts: 45
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

Joined: Aug 03, 2002
Posts: 7729
Have a close read of the API for the Comparable Interface


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
------------------------------------------------------------------
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

Joined: Feb 28, 2007
Posts: 1707
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,


cmbhatt
Jesse Custer
Ranch Hand

Joined: Feb 07, 2007
Posts: 45
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Treeset ordering