Is there a Java collection that removes elements as more elements are added? (simple cache)

I need a simple caching mechanism.

I want to keep up with the last 100 most recent items that have been added, but nothing more. When I add another item, I want the collection to automatically delete the 101st item. Imagine you pushing LifeSaver candies through the handset as I add another candy at one end of the tube and another candy will drop out at the other end.

It would be easy to write your own. My add method will first remove the oldest item before doing the add. I'm just wondering if there is already such a class.

I looked at the related classes of Collection, List, Queue, etc. I looked at Google Guava . But they do not have such a simple function.

This is a tricky topic for Google as I don’t know the jargon for this behavior.

0
java collections
Feb 11 '14 at 8:29
source share
1 answer

Apache Commons - CircularFifoQueue

CircularFifoQueue is the first queue in a fixed-size queue that replaces its oldest element if it is full.

I think this is the behavior you are looking for. Its from the collections of Apache collections ( link )

+9
Feb 11 '14 at 8:34
source share



All Articles