The Java Specialists' Newsletter
Issue 2432016-11-30 Category: Tips and Tricks Java version: 8
Subscribe RSS Feed

Hacking together a CircularArrayList (Video Tutorials)
by Dr. Heinz M. Kabutz
Abstract:
For our 16th anniversary edition, we have produced three short video tutorials on how to build your own CircularArrayList in Java, based on the AbstractList.


Welcome to the 243rd edition of The Java(tm) Specialists' Newsletter, sent to you from Crete. 16 years ago to this day, I sent out my first edition. My colleagues laughed at me. Hubris, one of the three "great virtues" of a programmer, together with laziness and impatience, has always been mine in abundance. After all, who dares to call their first edition "The Java(tm) Specialists' Newsletter"? And yet, it was with fear and trepidation that I clicked send to 80 Java programmers I had gleaned from my address book.

To celebrate our 16th anniversary, I've produced three video tutorials to show you how to write your own ArrayList and to then make it circular. We end off with a demo of how to test the code more thoroughly using reflection.

NEW: We have revised our "Advanced Topics" course, covering Reflection, Java NIO, Data Structures, Memory Management and several other useful topics for Java experts to master. 2 days of extreme fun and learning. Extreme Java - Advanced Topics.

Hacking together a CircularArrayList (Video Tutorials)

In this first episode, we code a simple ArrayList in under 10 minutes in preparation for a more complicated episode 2, where we code a CircularArrayList. We use the Apache Commons Collections test cases to ensure that our code is hopefully correct. All we need to do is override 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, where you can find all sorts of interesting queues like single or multiple producer/consumer, bounded or unbounded, depending on your particular requirements. His work is based on Martin Thompson's Mechanical Sympathy.

Java reflection belongs in the toolbox of every Java programmer. In this tutorial, we look at how we used it to thoroughly test our CircularArrayList.

Please let me know if you liked the tutorials by leaving comments and following me on Vimeo.

Kind regards

Heinz

Tips and Tricks Articles Related Java Course

Would you like to receive our monthly Java newsletter, with lots of interesting tips and tricks that will make you a better Java programmer?