What is the best implementation of a Circular Buffer in Java? I have read other questions but they're old and refer to CircularFifoBuffer which isn't present in Apache Commons Collections 4. Is there a new, widely accepted way to use a circular buffer in Java 8?
Asked
Active
Viewed 3,021 times
1
-
2Well, there's [CircularFifoQueue](https://commons.apache.org/proper/commons-collections/javadocs/api-4.2/org/apache/commons/collections4/queue/CircularFifoQueue.html) which might be want you want. Btw, the "circular-dependency" tag seems to be wrong here - or did I miss something? – Thomas Jan 14 '19 at 16:37
-
Seems you forgot to read [the answer by T. Baum](https://stackoverflow.com/a/11842123/5772882)? – Ole V.V. Jan 14 '19 at 16:47
-
I think the CircularFifoBuffer was removed to CircularFifoQueue, thank you very much! About the tag, it was definitely a mistake, I apologize. – user3804769 Jan 15 '19 at 08:42
1 Answers
0
java.util.ArrayDeque. The class is still a bit overlooked.
From the documentation:
Resizable-array implementation of the
Deque
interface. Array deques have no capacity restrictions; they grow as necessary to support usage. … This class is likely to be faster thanStack
when used as a stack, and faster thanLinkedList
when used as a queue.Since:
1.6
The documentation doesn’t state directly that it’s circular, but it is (I just inspected the source code to be 100 % sure). Since they recommend using it as a queue, it doesn’t really make sense not to make it circular.

Ole V.V.
- 81,772
- 15
- 137
- 161
-
2The quote from documentation says _`Array deques have no capacity restrictions`_ which means it will grow unbounded. So, it is completely opposite to the requirement in question - circular buffers will wrap and will not grow beyond specified limits. – uvsmtid May 11 '20 at 19:04
-
@uvsmtid You may be right that I have stretched the definition of *circular buffer* a bit. – Ole V.V. May 11 '20 at 19:17