views:

229

answers:

2

I need something like a bounded queue where I can only insert say 10 elements. A new element should override the last inserted element. There should be only n distinct elements

I am looking for suggestions in Java by the way.

+4  A: 

ITYM an implementation of a LRU algorithm.

dirkgently
@Rich B: That was an intentional miss. If GIYF questions are allowed, so should be such answers. However, the reason for the miss was so that the OP actually finds it out. You don't expect FAQs to be repeated time and again, do you?
dirkgently
+5  A: 

This is a classic application for a queue data structure. A stack of size 10 would keep track of the first 10 elements you added to it, whereas a queue of size 10 would keep track of the 10 most recent elements. If you are looking for a "recent items" list, as suggested by the title of your question, then a queue is the way to go.

edit: Here is an example, for visualization purposes:

Let's say you want to keep track of the most recent 4 items, and you access eight items in the following order:

F, O, R, T, Y, T, W, O

Here is what your data structures would look like over time:

access  item      queue          stack
  1      F     [ F . . . ]    [ . . . F ]
  2      O     [ O F . . ]    [ . . O F ]
  3      R     [ R O F . ]    [ . R O F ]
  4      T     [ T R O F ]    [ T R O F ]
  5      Y     [ Y T R O ]    [ Y R O F ]
  6      T     [ T Y T R ]    [ T R O F ]
  7      W     [ W T Y T ]    [ W R O F ]
  8      O     [ O W T Y ]    [ O R O F ]

Removing the top element from a stack removes the item that was most recently added, not the oldest item.

Removing one element from a queue removes the oldest item, so your queue will automatically hold the most recent items.

edit: with thanks to Adam Jaskiewicz, here is some documentation on Java's Queue implementation.

e.James
I know I should use queue, I wonder if Java has the container I need...
A queue backed by a mechanism to keep track of multiple accesses to the same element...
Manrico Corazzi
The OP said: A new element should override the **last** inserted element. That is last in, first out. He does not want to see the 10 most recent, unless he worded his question incorrectly.
Kevin Crowell
@Kevin Crowell: I think that is the case. The title of his questions mentions a "recent items" list, so I think he used "last" to mean "oldest".
e.James
@Sasha: You'll have to write your own, but it isn't too difficult. I have added a link to one example.
e.James
Java *DOES* have a queue. The queue interface has existed in Java since 1.5 (2004), and LinkedList, among other Collections, implements it.
Adam Jaskiewicz
That article references the 1.4 Javadocs, so it is quite out of date. Shame on developer.com for not putting datestamps on their articles.
Adam Jaskiewicz
@Adam Jaskiewicz: That's my lack of Java experience showing. Do you have a link to the documentation?
e.James
And that's a pretty dumb way to implement a queue, for a number of reasons.
Adam Jaskiewicz
Here you go: http://java.sun.com/javase/6/docs/technotes/guides/collections/reference.html
Adam Jaskiewicz
Oh, read the second page of the article; it does go on to implement it using LinkedList instead of Vector, so that's covered; it's just out of date because LinkedList now has methods that do what he is adding.
Adam Jaskiewicz
@Adam Jaskiewicz: Thank you kindly; I have added your link to the question.
e.James