Size-limited queue that holds last N elements in Java
Apache commons collections 4 has a CircularFifoQueue<> which is what you are looking for. Quoting the javadoc: CircularFifoQueue is a first-in first-out queue with a fixed size that replaces its oldest element if full. import java.util.Queue; import org.apache.commons.collections4.queue.CircularFifoQueue; Queue<Integer> fifo = new CircularFifoQueue<Integer>(2); fifo.add(1); fifo.add(2); fifo.add(3); System.out.println(fifo); // Observe the result: // [2, 3] If … Read more