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, this newsletter is a bit different. Instead of the usual, I'm sending you three tutorials that I did last week to show how easy it is to write your own ArrayList and to then make it circular. We end off with a look at how to test the code more thoroughly using reflection.
Attend our premium Extreme Java - Concurrency Performance for Java 8 Course from the comfort of your own chair :-)
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.