SortedList: best practices with java 1.5

  • Thread starter B-rad the Beat Nick
  • Start date
B

B-rad the Beat Nick

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
 
B

Benji

B-rad the Beat Nick said:
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.
 
J

Joan

B-rad the Beat Nick said:
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

This is best for me ;-)

import java.util.TreeMap;
 
G

Googmeister

Joan said:
This is best for me ;-)

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.
 
R

Roedy Green

You can be lazy and just tack them on the end, and only sort when you
need the sorted order.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top