What Collection to use?

Discussion in 'Java' started by Philipp, Jan 29, 2007.

  1. Philipp

    Philipp Guest

    Dear all,

    My app can load files using a FileChooser. I would like to store the
    last 4 loaded file pathes so I can use them later (ie fast access to
    last used files). I'm looking for the perfect Collection to store these
    4 File objects.
    I think it should have the following properties
    1. no duplicate objects
    2. knowing the insert order (so you know which was last entered)
    3. limited size (4 objects) and throwing out the oldest entry when
    inserting a new one (like a ring buffer maybe)

    Is there such a Collection already or do I have to homecook one?

    Thanks for your answers
    Phil

    PS: Is there a summary table on the web with all properties of the
    standard subclasses of Collection and Map?
     
    Philipp, Jan 29, 2007
    #1
    1. Advertising

  2. Philipp

    ceasaro Guest

    Not in the standard developer kid. point 3 is the hard one here. But
    there is a possibility you find the collection you need in the http://
    jakarta.apache.org/commons/collections/ api. Maybe the FixedFileList
    will meet your expectations see the javadoc for more info http://
    jakarta.apache.org/commons/collections/api-release/index.html

    On Jan 29, 10:02 pm, Philipp <> wrote:
    > Dear all,
    >
    > My app can load files using a FileChooser. I would like to store the
    > last 4 loaded file pathes so I can use them later (ie fast access to
    > last used files). I'm looking for the perfect Collection to store these
    > 4 File objects.
    > I think it should have the following properties
    > 1. no duplicate objects
    > 2. knowing the insert order (so you know which was last entered)
    > 3. limited size (4 objects) and throwing out the oldest entry when
    > inserting a new one (like a ring buffer maybe)
    >
    > Is there such a Collection already or do I have to homecook one?
    >
    > Thanks for your answers
    > Phil
    >
    > PS: Is there a summary table on the web with all properties of the
    > standard subclasses of Collection and Map?
     
    ceasaro, Jan 29, 2007
    #2
    1. Advertising

  3. Philipp wrote:
    > Dear all,
    >
    > My app can load files using a FileChooser. I would like to store the
    > last 4 loaded file pathes so I can use them later (ie fast access to
    > last used files). I'm looking for the perfect Collection to store these
    > 4 File objects.
    > I think it should have the following properties
    > 1. no duplicate objects
    > 2. knowing the insert order (so you know which was last entered)
    > 3. limited size (4 objects) and throwing out the oldest entry when
    > inserting a new one (like a ring buffer maybe)
    >
    > Is there such a Collection already or do I have to homecook one?


    #1 is a bit of a problem, because it excludes the closest collections. A
    LinkedHashSet iterates in the order of original insertion, so a re-load
    of the oldest file would not move it from being next to delete.

    I would probably get rid of that property, and use a LinkedList wrapped
    in a class that checks, every time it adds a new item, whether the
    size() is greater than four, and if so deletes the first item.

    Patricia
     
    Patricia Shanahan, Jan 29, 2007
    #3
  4. Philipp

    Philipp Guest

    I forgot:

    > 1. no duplicate objects
    > 2. knowing the insert order (so you know which was last entered)
    > 3. limited size (4 objects) and throwing out the oldest entry when
    > inserting a new one (like a ring buffer maybe)

    4. when re-inserting an already present object, it should move to the front


    this makes LinkedHashSet not suitable :-(
     
    Philipp, Jan 29, 2007
    #4
  5. Philipp

    Ian Pilcher Guest

    java.io.File[4]?

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
     
    Ian Pilcher, Jan 29, 2007
    #5
  6. Philipp

    Daniel Pitts Guest

    On Jan 29, 1:32 pm, Philipp <> wrote:
    > I forgot:
    >
    > > 1. no duplicate objects
    > > 2. knowing the insert order (so you know which was last entered)
    > > 3. limited size (4 objects) and throwing out the oldest entry when
    > > inserting a new one (like a ring buffer maybe)

    >
    > 4. when re-inserting an already present object, it should move to the front
    >
    > this makes LinkedHashSet not suitable :-(


    class MostRecentList implements Serializable {
    private final int maxSize;
    private final LinkedList<String> list = new LinkedList<String>();
    public void add(String item) {
    if (list.contains(item)) {
    list.remove(item);
    }
    list.addFirst(item);
    while (list.size() > maxSize()) {
    list.removeLast();
    }
    }
    }
     
    Daniel Pitts, Jan 29, 2007
    #6
  7. Philipp

    Lee Weiner Guest

    In article <>, Philipp <> wrote:
    >Dear all,
    >
    >My app can load files using a FileChooser. I would like to store the
    >last 4 loaded file pathes so I can use them later (ie fast access to
    >last used files). I'm looking for the perfect Collection to store these
    >4 File objects.
    >I think it should have the following properties
    >1. no duplicate objects
    >2. knowing the insert order (so you know which was last entered)
    >3. limited size (4 objects) and throwing out the oldest entry when
    >inserting a new one (like a ring buffer maybe)
    >
    >Is there such a Collection already or do I have to homecook one?


    What you're describing is generally known as a Most Recently Used (MRU) list.
    I have an MRU class that uses a plain ol' ArrayList. I add the new one to the
    top of the list, then use indexOf() to check whether it's already in the list,
    and delete it from its old position if it is. Whether I delete one or not, if
    the list is now longer than 4 elements, delete one from from the bottom of the
    list.

    Lee Weiner
    lee AT leeweiner DOT org
     
    Lee Weiner, Jan 30, 2007
    #7
  8. Philipp

    Philipp Guest

    Daniel Pitts wrote:
    >
    > On Jan 29, 1:32 pm, Philipp <> wrote:
    >> I forgot:
    >>
    >>> 1. no duplicate objects
    >>> 2. knowing the insert order (so you know which was last entered)
    >>> 3. limited size (4 objects) and throwing out the oldest entry when
    >>> inserting a new one (like a ring buffer maybe)

    >> 4. when re-inserting an already present object, it should move to the front
    >>
    >> this makes LinkedHashSet not suitable :-(

    >
    > class MostRecentList implements Serializable {
    > private final int maxSize;
    > private final LinkedList<String> list = new LinkedList<String>();
    > public void add(String item) {
    > if (list.contains(item)) {
    > list.remove(item);
    > }
    > list.addFirst(item);
    > while (list.size() > maxSize()) {
    > list.removeLast();
    > }
    > }
    > }
    >


    Wow! Thanks for that ready made solution! :)
    Phil
     
    Philipp, Jan 30, 2007
    #8
  9. Philipp

    Chris Uppal Guest

    Daniel Pitts wrote:

    > class MostRecentList implements Serializable {
    > private final int maxSize;
    > private final LinkedList<String> list = new LinkedList<String>();
    > public void add(String item) {
    > if (list.contains(item)) {
    > list.remove(item);
    > }
    > list.addFirst(item);
    > while (list.size() > maxSize()) {
    > list.removeLast();
    > }
    > }
    > }


    Nice demo of why there is little need for this kind of functionality to be
    pre-packaged in the standard libraries.

    Minor nit: the above would be even shorter and sweeter if you deleted the
    if (list.contains(item))
    test -- there is no need for it since an uncondition call to remove() would
    have the same effect.

    -- chris
     
    Chris Uppal, Jan 30, 2007
    #9
  10. Philipp

    Daniel Pitts Guest

    On Jan 30, 7:18 am, "Chris Uppal" <-
    THIS.org> wrote:
    > Daniel Pitts wrote:
    > > class MostRecentList implements Serializable {
    > > private final int maxSize;
    > > private final LinkedList<String> list = new LinkedList<String>();
    > > public void add(String item) {
    > > if (list.contains(item)) {
    > > list.remove(item);
    > > }
    > > list.addFirst(item);
    > > while (list.size() > maxSize()) {
    > > list.removeLast();
    > > }
    > > }
    > > }

    >
    > Nice demo of why there is little need for this kind of functionality to be
    > pre-packaged in the standard libraries.
    >
    > Minor nit: the above would be even shorter and sweeter if you deleted the
    > if (list.contains(item))
    > test -- there is no need for it since an uncondition call to remove() would
    > have the same effect.
    >
    > -- chris

    Indeed.

    Actually, if I were creating this for real, I would implement a class
    MostRecentList<T> extend AbstractList<T>. Possibly using a fixed
    length object array as the backing store, or an ArrayList<T>.
     
    Daniel Pitts, Jan 30, 2007
    #10
    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. Dylan
    Replies:
    5
    Views:
    432
    Daniel T.
    Mar 22, 2005
  2. Pradeep
    Replies:
    2
    Views:
    687
    Patricia Shanahan
    Jan 24, 2007
  3. Øyvind Isaksen
    Replies:
    1
    Views:
    1,000
    Øyvind Isaksen
    May 18, 2007
  4. Hemant

    create collection of collection

    Hemant, Oct 22, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    428
    Gregory A. Beamer
    Oct 22, 2009
  5. Don
    Replies:
    0
    Views:
    150
Loading...

Share This Page