I found some prior threads on this group regarding SortedLists, but
they were all published around 2003 or before.
What is the best practices way to implement a SortedList with Java 1.5?
I saw the technique using Collections.binarySearch() method to identify
the insertion point. In terms of speed, is that faster than simply
calling Collections.sort() on every add()? I did a fair amount of
googling and searching of this group without finding any RECENT
discussions (since 1.5) regarding this subject.
Thanks,
B
Benji - 04 Nov 2005 06:10 GMT
> What is the best practices way to implement a SortedList with Java 1.5?
Depends on how you're going to be using it. If you're going to be
occasionally adding to the collection, and need it to always be sorted,
I would just implement a linked list and an in-order add method.
If you're going to be adding large amounts of data at a time, and only
need it to be sorted in between batch adds, I would just use an ArrayList,
and call Collections.sort() after you're done adding.

Signature
Of making better designs there is no end,
and much refactoring wearies the body.
Roedy Green - 04 Nov 2005 06:35 GMT
On 3 Nov 2005 20:57:49 -0800, "B-rad the Beat Nick"
<bradley.wagner@gmail.com> wrote, quoted or indirectly quoted someone
who said :
>What is the best practices way to implement a SortedList with Java 1.5?
I have written a SortedArrayList class. See
http://mindprod.com/products1.html#SORTED

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Joan - 04 Nov 2005 17:20 GMT
>I found some prior threads on this group regarding SortedLists,
>but
[quoted text clipped - 14 lines]
> Thanks,
> B
This is best for me ;-)
import java.util.TreeMap;
Googmeister - 04 Nov 2005 23:58 GMT
> >I found some prior threads on this group regarding SortedLists,
> >but
[quoted text clipped - 18 lines]
>
> import java.util.TreeMap;
Except that's it (and its cousin TreeSet) do not allow duplicate keys.
So you'd have to tweak it to make it work here, but I agree that
a BST is a fine choice in general.
Roedy Green - 05 Nov 2005 09:50 GMT
>> I saw the technique using Collections.binarySearch() method to
>> identify
[quoted text clipped - 4 lines]
>> googling and searching of this group without finding any RECENT
>> discussions (since 1.5) regarding this subject.
You can be lazy and just tack them on the end, and only sort when you
need the sorted order.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.