Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Treeset ordering

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a close read of the API for the Comparable Interface
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic