What Collection to use?

P

Philipp

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?
 
C

ceasaro

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
 
P

Patricia Shanahan

Philipp said:
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
 
P

Philipp

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 :-(
 
D

Daniel Pitts

I forgot:


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();
}
}
}
 
L

Lee Weiner

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
 
P

Philipp

Daniel said:
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
 
C

Chris Uppal

Daniel said:
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
 
D

Daniel Pitts

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top