What Collection to use?  
Author Message
Philipp





PostPosted: 2007-1-30 5:03:00 Top

java-programmer, What Collection to use? 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





PostPosted: 2007-1-30 5:09:00 Top

java-programmer >> What Collection to use? 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 <email***@***.com> 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?

 
Patricia Shanahan





PostPosted: 2007-1-30 5:28:00 Top

java-programmer >> What Collection to use? 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

 
 
Philipp





PostPosted: 2007-1-30 5:33:00 Top

java-programmer >> What Collection to use? 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 :-(

 
 
Ian Pilcher





PostPosted: 2007-1-30 6:02:00 Top

java-programmer >> What Collection to use? java.io.File[4]?

--
========================================================================
Ian Pilcher email***@***.com
========================================================================
 
 
Daniel Pitts





PostPosted: 2007-1-30 6:08:00 Top

java-programmer >> What Collection to use?

On Jan 29, 1:32 pm, Philipp <email***@***.com> 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();
}
}
}

 
 
lee





PostPosted: 2007-1-30 10:31:00 Top

java-programmer >> What Collection to use? In article <email***@***.com>, Philipp <email***@***.com> 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
 
 
Philipp





PostPosted: 2007-1-30 15:39:00 Top

java-programmer >> What Collection to use? Daniel Pitts wrote:
>
> On Jan 29, 1:32 pm, Philipp <email***@***.com> 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
 
 
Chris Uppal





PostPosted: 2007-1-30 23:18:00 Top

java-programmer >> What Collection to use? 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


 
 
Daniel Pitts





PostPosted: 2007-1-31 2:28:00 Top

java-programmer >> What Collection to use? On Jan 30, 7:18 am, "Chris Uppal" <email***@***.com
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>.