Java collection containing only n of the last inserted elements

As the title says, I am looking for a java collection containing only N of the last objects inserted into the collection. This FIFO collection does not require random access or support N.

All collections that I can find are either blocked ( LinkedBlockingQueue ) or unlimited in size ( ArrayDeque ). I found org.eclipse.jetty.util.ArrayQueue, but, as you might have guessed, this brings a rather undesirable dependency on my project, and is also very difficult, since it supports changing N, so it is not what I need.

Do you know if there is a way to get this with a fairly common java library, or do I need to write it myself?

+5
source share
3 answers

Check out the Apache Commons CircularFifoBuffer

CircularFifoBuffer is the first in the first fixed-size buffer that replaces its oldest element if it is full.

CircularFifoBuffer removal order is based on an insert order; items are deleted in the same order in which they were added. The iteration order is the same as the delete order.

+6
source

-, Queue Deque , , , , . , , Queue Deque, , , .

+2

After Guava 15.0 there is an EvictingQueue that comes with a fixed size that replaces its oldest element if it is full.

+1
source

All Articles