Search code examples
javaqueuecircular-queue

can someone please tell is there any java built in package for Circular Queue?


I want to know that is there any java built-in package for a circular queue and if it exists then what is the constructor to use it?


Solution

  • You could use Class CircularFifoBuffer from apache to build a buffer with a fixed size that replaces its oldest element if full.

    The constructor would look like this:

    Buffer circularQueue = new CircularFifoBuffer(size);
    

    From de official documentation:

    public class CircularFifoBuffer extends BoundedFifoBuffer

    CircularFifoBuffer is a first in first out buffer with a fixed size that replaces its oldest element if full. The removal order of a CircularFifoBuffer is based on the insertion order; elements are removed in the same order in which they were added. The iteration order is the same as the removal order.

    The add(Object), BoundedFifoBuffer.remove() and BoundedFifoBuffer.get() operations all perform in constant time. All other operations perform in linear time or worse.

    Note that this implementation is not synchronized. The following can be used to provide synchronized access to your CircularFifoBuffer:

    Check the documentation: public class CircularFifoBuffer