aspose file tools
The moose likes Java in General and the fly likes Collection implementation sorting by Comparable when calling add() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Collection implementation sorting by Comparable when calling add()" Watch "Collection implementation sorting by Comparable when calling add()" New topic
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
    
    4
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
    
    4

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
    
    4
You're welcome
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3036
    
    4

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collection implementation sorting by Comparable when calling add()
 
Similar Threads
Comparable And Comparator usage in real time
ArrayList class
can anybody tell about comparator in java
Natural Order
Matually Comparable