Home of The JavaSpecialists' Newsletter

Java Tutorials by Dr Heinz Kabutz

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.

Java Tutorial 002 - Making the ArrayList circular in under 15 minutes

Previous          Next

Previous          Next

We hope you enjoyed this tutorial. If you did, you will also enjoy our courses. We suggest you start with Extreme Java - Advanced Java, followed by Extreme Java - Concurrency Performance for Java 8.



About the Author

demo

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

Nobody ever wants to call a Java performance consultant, but with first-hand experience repairing and improving commercial Java applications - JavaSpecialists are a good place to start...

Threading Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.