In our previous tutorial we wrote a basic ArrayList by overriding just 5 methods from AbstractList. We now expand this by adding a "head" field to our class. This allows us to wrap the elements around the array, hence our name CircularArrayList. It also means that we can add/remove from both ends of the list in constant time.
Our end result is not thread safe. If you need fast thread-safe queues, check out Nitsan Wakart's JCTools based on Martin Thompson's Mechanical Sympathy where you can find all sorts of interesting queues like single or multiple producer/consumer, bounded or unbounded, depending on your particular requirements.
Testing the CircularArrayList is not trivial as there are so many cases to consider as the head moves around the array. In our third tutorial we look at how we can use Java reflection to thoroughly test our class.
Would you like to receive our monthly Java newsletter, with lots of interesting tips and tricks that will make you a better Java programmer?