-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Possible bug: SortedSet uses IComparable for its Add/Remove?
PostPosted: Sun Dec 04, 2005 6:59 pm 
I am cross-posting this at...

http://www.codeproject.com/csharp/sets.asp?forumid=13500&exp=0&select=1296521&df=100&msg=1296521

...because I am not sure whose responsibility it should become.

I have a query about the SortedSet implementation. At present, it uses a SortedList as its backing store? However, I do not believe that, semantically, a SortedList is equivalent to a SortedSet?

Specially, as can be seen at http://www.dotnet247.com/247reference/System/Collections/SortedList/__rotor, SortedList uses IComparable and BinarySearch, not GetHashCode() and Equals(), to implement its Add, Contains, etc.

I have a class whose GetHashCode() and Equals() methods key off one property of the object, and whose CompareTo() keys off another. This is because my GetHashCode() and Equals() method use internal, unique identifiers useful to the code whereas my CompareTo() method uses human-readable, non-unique identifiers useful to people.

I believe this is a valid use case: I do not believe the contract of CompareTo() requires it use the same identifier as GetHashCode() and Equals()?

However, SortedSet breaks in this scenario because (I believe) it goes looking for an existing object using CompareTo() rather than by GetHashCode() and Equals(). This means it is possible to put multiple items into a SortedSet whose Equals() methods return true, which breaks the ISet contract (though of course is perfectly valid for a SortedList).

Have I gotten this completely wrong? Is there a better approach?

Regards,

Richard.


Top
  
 
 Post subject:
PostPosted: Mon Dec 05, 2005 11:05 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
SortedSet in Java also uses IComparable (Comparable in Java), the documentation is a bit vague on what happens if elements are compared equal by compareTo but not by equals, but I think the behavior is the same as the .NET implementation currently has.

See http://java.sun.com/j2se/1.4.2/docs/api/java/util/SortedSet.html

I think you should just provide a comparator which will compare two elements equal only if they are really equal.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.