Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

What Collection to use?

Thread view: 
Philipp - 29 Jan 2007 21:02 GMT
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 - 29 Jan 2007 21:08 GMT
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

> Dear all,
>
[quoted text clipped - 15 lines]
> PS: Is there a summary table on the web with all properties of the
> standard subclasses of Collection and Map?
Patricia Shanahan - 29 Jan 2007 21:28 GMT
> Dear all,
>
[quoted text clipped - 9 lines]
>
> 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
Philipp - 29 Jan 2007 21:32 GMT
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 :-(
Daniel Pitts - 29 Jan 2007 22:07 GMT
> I forgot:
>
[quoted text clipped - 6 lines]
>
> 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();
     }
  }
}
Philipp - 30 Jan 2007 07:38 GMT
>> I forgot:
>>
[quoted text clipped - 19 lines]
>    }
> }

Wow! Thanks for that ready made solution! :-)
Phil
Chris Uppal - 30 Jan 2007 15:18 GMT
> class MostRecentList implements Serializable {
>    private final int maxSize;
[quoted text clipped - 9 lines]
>    }
> }

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
Daniel Pitts - 30 Jan 2007 18:27 GMT
On Jan 30, 7:18 am, "Chris Uppal" <chris.up...@metagnostic.REMOVE-
THIS.org> wrote:
> > class MostRecentList implements Serializable {
> >    private final int maxSize;
[quoted text clipped - 19 lines]
>
>     -- 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>.
Ian Pilcher - 29 Jan 2007 22:02 GMT
java.io.File[4]?

Signature

========================================================================
Ian Pilcher                                         arequipeno@gmail.com
========================================================================

Lee Weiner - 30 Jan 2007 02:31 GMT
>Dear all,
>
[quoted text clipped - 9 lines]
>
>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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.