I need a queue with a fixed size. When I add an element and the queue is full, it should automatically remove the oldest element.
Is there an existing implementation for this in Java?
javaqueue
I need a queue with a fixed size. When I add an element and the queue is full, it should automatically remove the oldest element.
Is there an existing implementation for this in Java?
Best Answer
Actually the LinkedHashMap does exactly what you want. You need to override the
removeEldestEntry
method.Example for a queue with max 10 elements:
If the "removeEldestEntry" returns true, the eldest entry is removed from the map.