SortedList: best practices with java 1.5

Discussion in 'Java' started by B-rad the Beat Nick, Nov 4, 2005.

  1. 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-rad the Beat Nick, Nov 4, 2005
    #1
    1. Advertising

  2. B-rad the Beat Nick

    Benji Guest

    B-rad the Beat Nick, while high on whiteboard markers, wrote:
    > 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.

    --
    Of making better designs there is no end,
    and much refactoring wearies the body.
    Benji, Nov 4, 2005
    #2
    1. Advertising

  3. B-rad the Beat Nick

    Roedy Green Guest

    On 3 Nov 2005 20:57:49 -0800, "B-rad the Beat Nick"
    <> 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
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 4, 2005
    #3
  4. B-rad the Beat Nick

    Joan Guest

    "B-rad the Beat Nick" <> wrote in message
    news:...
    >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;
    Joan, Nov 4, 2005
    #4
  5. B-rad the Beat Nick

    Googmeister Guest

    Joan wrote:
    > "B-rad the Beat Nick" <> wrote in message
    > news:...
    > >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;


    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.
    Googmeister, Nov 4, 2005
    #5
  6. B-rad the Beat Nick

    Roedy Green Guest

    On Fri, 4 Nov 2005 10:20:25 -0600, "Joan" <> wrote,
    quoted or indirectly quoted someone who said :

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


    You can be lazy and just tack them on the end, and only sort when you
    need the sorted order.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 5, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski

    Problem with SortedList

    Nathan Sokalski, May 30, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,875
    Karl Seguin
    May 30, 2005
  2. Thomas Weidenfeller

    Re: SortedList ?

    Thomas Weidenfeller, Jul 1, 2003, in forum: Java
    Replies:
    3
    Views:
    2,861
    Tim Tyler
    Jul 10, 2003
  3. Phil Hanna

    Re: SortedList ?

    Phil Hanna, Jul 2, 2003, in forum: Java
    Replies:
    0
    Views:
    820
    Phil Hanna
    Jul 2, 2003
  4. sck10
    Replies:
    4
    Views:
    420
    sck10
    Sep 26, 2006
  5. jc
    Replies:
    1
    Views:
    704
    sloan
    Jul 15, 2008
Loading...

Share This Page