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?
-
[First google hit…](https://www.javainuse.com/java/circular_java) I think you have to implement one yourself or use an existing library. – deHaar Feb 11 '19 at 12:09
-
This seems like a duplicate of https://stackoverflow.com/questions/29803724/circular-linkedlist-in-java – Norbert Bicsi Feb 11 '19 at 12:10
-
i can implement it but i want to know if there is any library exist in java or not? – Feb 11 '19 at 12:11
-
No there isn't. Have you tried googling it though? – Norbert Bicsi Feb 11 '19 at 12:18
1 Answers
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

- 1,080
- 1
- 12
- 24
-
It isn't really build it if it is a third party apache library now is it? But yeah, this exists I guess. – Norbert Bicsi Feb 11 '19 at 12:22
-
1