CircularArrayList for Java - Museful
Java applications sometimes need a "sliding window" view on a stream/sequence of data. This can be provided by a List with
- fast element insertion and removal at opposite ends (like a queue)
- fast access to random elements in the list (like an array)
Unfortunately, no existing implementation of the List interface in the Java Collections Framework satisfies both of these requirements. ArrayList is slow at inserting/removing at the list's head, while LinkedList is slow at accessing elements in the list's interior.
(ArrayDeque in the Java Collections Framework almost works, but its designers did not implement the List interface, thereby (perhaps intentionally) hiding its interior elements.)
This problem can be solved by using a circular array (also called a circular buffer, cyclic buffer, or ring buffer) as the underlying data structure.
So here is the solution: a fully-fledged, ready-to-use implementation supporting generic element type and everything else you would expect from a standard implementation in the Java Collections Framework. By extending AbstractList and wrapping ArrayList, the full implementation is accomplished in under 100 lines of code!
Read full article from CircularArrayList for Java - Museful
No comments:
Post a Comment