Possible Duplicate:
Size-limited queue that holds last N elements in Java
Java – Ring Buffer
I am interested in a bounded above queue, that whenever faced with object insertion, would remove the oldest object first, if the insertion would result in ‘overflowing’. I want the addition to be O(1) and the memory usage as little as possible. I was thinking about either overriding add method on LinkedList, but ideally I would implement a circular, array based list, with catching front/back pointer. Whenever the addition is made over capacity, front pointer advances, and then the back one. Is there an implementation similar to this?
A linked list is a waste of memory, since the next pointer uses mem, that the ArrayList does not.
The performant implementations are based on ArrayList or better on an array.
If your circular buffer size is fixed, you would use an array.
I implemented a circular buffer using an internal array, with start and end position index vars. I did not found an implemnetation of a circular list / buffer, that did that what i wanted.
It was not dificullt to implement, but i recomend using a high number of unit test cases, to prove that your circ buffer works as expected.