Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > What Collection to use?

Reply
Thread Tools

What Collection to use?

 
 
Philipp
Guest
Posts: n/a
 
      01-29-2007
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?


 
Reply With Quote
 
 
 
 
ceasaro
Guest
Posts: n/a
 
      01-29-2007
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 <(E-Mail Removed)> 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?


 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      01-29-2007
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

 
Reply With Quote
 
Philipp
Guest
Posts: n/a
 
      01-29-2007
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

 
Reply With Quote
 
Ian Pilcher
Guest
Posts: n/a
 
      01-29-2007
java.io.File[4]?

--
================================================== ======================
Ian Pilcher http://www.velocityreviews.com/forums/(E-Mail Removed)
================================================== ======================
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      01-29-2007


On Jan 29, 1:32 pm, Philipp <(E-Mail Removed)> 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();
}
}
}

 
Reply With Quote
 
Lee Weiner
Guest
Posts: n/a
 
      01-30-2007
In article <(E-Mail Removed)>, Philipp <(E-Mail Removed)> 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
 
Reply With Quote
 
Philipp
Guest
Posts: n/a
 
      01-30-2007
Daniel Pitts wrote:
>
> On Jan 29, 1:32 pm, Philipp <(E-Mail Removed)> 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
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      01-30-2007
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


 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      01-30-2007
On Jan 30, 7:18 am, "Chris Uppal" <(E-Mail Removed)-
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>.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
create collection of collection Hemant ASP .Net 1 10-22-2009 03:04 PM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
Sorting the Collection(java.util.Collection) of Elements Pradeep Java 2 01-24-2007 02:33 PM
Adding to collection from controlDesigner only produces end tag for the collection. Don ASP .Net Building Controls 0 07-22-2005 09:31 PM
STL - an algorithm for finding a collection within a collection? Dylan C++ 5 03-22-2005 01:31 AM



Advertisments