Logical inconsistence between Set and SortedSet interfaces
peter.levart at gmail.com
Sun Jan 27 11:34:07 UTC 2013
You can also create your own java.util.Comparator implementation that
never returns 0 for two "different" keys. In your example, you would
have to break the ties by 1st comparing the desired attribute(s) that
you want to order by and then some unique attribute - you might want to
add it to the key just for that purpose (a sequence for example,
allocated transiently in the VM - don't use identityHashCode, it is not
On 01/27/2013 11:30 AM, Andrej Hollmann wrote:
> I want to add few products to the SortedSet and sort them by price. I
> add four different elements to TreeSet:
> ["salt",0.5$], ["milk", 1$], ["bread", 1$], ["bananas", 2$]
> But at the end my TreeSet contains only three elements:
> ["salt",0.5$], ["bread", 1$], ["bananas", 2$]
> The "bread" replaced the "milk" because it has the same price. I think
> ordering and equity are different aspects and should be separated. A
> logically correct SortedSet implementation should contain all four
> elements in ordered style. In that way SortedSet would be downward
> compatible to inherited Set interface.
> To test my thinks I wrote my own SortedSet, which is based on TreeList
> from apache-collection and achieved desired results.
> PS: With new SortedSet I can sort products by date, or supplier, ... .
> I just need to implement my own comparator and can be sure that added
> products will be not replaced by others because elements are the same
> during comparison.
> Best Regards
More information about the core-libs-dev