Running on Java 24-ea+21-2447 (Preview)
Home of The JavaSpecialists' Newsletter

243Hacking Together a CircularArrayList (Video Tutorials)

Author: Dr. Heinz M. KabutzDate: 2016-11-30Java Version: 8Category: Tips and Tricks
 

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.

javaspecialists.teachable.com: Please visit our new self-study course catalog to see how you can upskill your Java knowledge.

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

 

Comments

We are always happy to receive comments from our readers. Feel free to send me a comment via email or discuss the newsletter in our JavaSpecialists Slack Channel (Get an invite here)

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Related Articles

Browse the Newsletter Archive

About the Author

Heinz Kabutz Java Conference Speaker

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

Superpack '23

Superpack '23 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

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

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...