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 Lambdas and Streams 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 Lambdas and Streams Workshop.
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.
Would you like to receive our monthly Java newsletter, with lots of interesting tips and tricks that will make you a better Java programmer?