tags:

views:

117

answers:

6

Hi,

I need a Queue with a fixed size. When I add an element and the the queue would overflow the fixed size it should automaticly remove the oldest element.

Is there an existing implementation for this in Java or do I have to programm my own?

Thanks in advance.

A: 

Sounds like an ordinary List where the add method contains an extra snippet which truncates the list if it gets too long.

If that is too simple, then you probably need to edit your problem description.

Thorbjørn Ravn Andersen
Actually, he would need to delete the first element (i.e. earliest), truncating would remove the last element. Still practical with a LinkedList.
MAK
+3  A: 

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 :-)

moritz
Use Queue instead of AbstractQueue... there may be queues that implement the interface but do not extend the abstract class.
TofuBeer
of course, didn't think of that!
moritz
A: 

Also see this SO question, or ArrayBlockingQueue (be careful about blocking, this might be unwanted in your case).

Zoran Regvart
+1  A: 

I think what you're describing is a circular queue. Here is an example and here is a better one

Sergey
A: 

It is not quite clear what requirements you have that led you to ask this question. If you need a fixed size data structure, you might also want to look at different caching policies. However, since you have a queue, my best guess is that you're looking for some type of router functionality. In that case, I would go with a ring buffer: an array that has a first and last index. Whenever an element is added, you just increment the last element index, and when an element is removed, increment the first element index. In both cases, addition is performed modulo the array size, and make sure to increment the other index when needed, that is, when the queue is full or empty.

Also, if it is a router-type application, you might also want to experiment with an algorithm such as Random Early Dropping (RED), which drops elements from the queue randomly even before it gets filled up. In some cases, RED has been found to have better overall performance than the simple method of allowing the queue to fill up before dropping.

jk
+5  A: 

Actually the LinkedHashMap does exactly what you want. You need to override the removeEldestEntry method.

Example for a queue with max 10 elements:

  queue = new LinkedHashMap<Integer, String>()
  {
     @Override
     protected boolean removeEldestEntry(Entry<Integer, String> eldest)
     {
        return this.size() > 10;   
     }
  };

If the "removeEldestEntry" returns true, the eldest entry is removed from the map.

Mavrik
I've used this pattern before and it works well.
matt b