Home of The JavaSpecialists' Newsletter

243Hacking together a CircularArrayList (Video Tutorials)

Posted: 2016-11-30Category: Tips and TricksJava Version: 1.8+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: Refactoring to Java 8 Streams and Lambdas Workshop Are you currently using Java 6 or 7 and would like to see how Java 8 can improve your code base? Are you tired of courses that teach you a whole bunch of techniques that you cannot apply in your world? Check out our one day intensive Refactoring to Java 8 Streams and Lambdas Workshop.

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

 

Related Articles

Browse the Newsletter Archive

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.