Collection that loses oldest elements on add()

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


Apart from Linkedhasmap if you are looking for list type solution, Google guava has EvictingQueue. And for thread safety you must wrap it in a synchronized wrapper (Queues#synchronizedQueue).

EvictingQueue<String> q = EvictingQueue.create(3);
Queue<String> syncQ =  Queues.synchronizedQueue(q);
syncQ.add("one");
syncQ.add("two");
syncQ.add("three");
syncQ.add("four");
System.out.println(q); // Prints [two, three, four]

Tags:

Java