Skip to content
Advertisement

Is there a fixed sized queue which removes excessive elements?

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?

Advertisement

Answer

There is no existing implementation in the Java Language and Runtime. All Queues extend AbstractQueue, and its doc clearly states that adding an element to a full queue always ends with an exception. It would be best ( and quite simple ) to wrap a Queue into a class of your own for having the functionality you need.

Once again, because all queues are children of AbstractQueue, simply use that as your internal data type and you should have a flexible implementation running in virtually no time 🙂

UPDATE:

As outlined below, there are two open implementations available (this answer is quite old, folks!), see this answer for details.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement