| Author |
Collection implementation sorting by Comparable when calling add()
|
manuel aldana
Ranch Hand
Joined: Dec 29, 2005
Posts: 308
|
|
hi
does somebody know an implementation of list which is sorting when doing an add(), and using the Comparable.compareTo() internall. e.g. something like:
XXX.add(<T extends Comparable> item).
In a case can't be bothered to call Collections.sort() after adding operations.
|
aldana software engineering blog & .more
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
That doesn't sound like a List; you could write your own, but the idea of a List is that it retains insertion order.
There are sorted Sets: look for the SortedSet interface and its implementing classes.
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3036
|
|
|
Or, since Sets don't allow duplicates, perhaps a Queue, like the PriorityQueue
|
Steve
|
 |
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
|
|
|
How about one more suggestion to the mix, a TreeMap which also retains its order: TreeMap API
|
 |
manuel aldana
Ranch Hand
Joined: Dec 29, 2005
Posts: 308
|
|
thanks,
i think i will go for the SortedSet. the non-duplicate semantic is alright, items regarding equals() implementation are all unique.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
You're welcome
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3036
|
|
manuel aldana wrote:thanks,
i think i will go for the SortedSet. the non-duplicate semantic is alright, items regarding equals() implementation are all unique.
Be aware that SortedSets may determine equality on compareTo() returning 0 rather than equals(). From the API:
"a sorted set performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the sorted set, equal."
|
 |
manuel aldana
Ranch Hand
Joined: Dec 29, 2005
Posts: 308
|
|
yeah, but it is no problem, because compareTo returning 0 is like equals() returning true in my implementation.
|
 |
 |
|
|
subject: Collection implementation sorting by Comparable when calling add()
|
|
|