Java Generics and Collections – 2e!

Yesterday I heard that O’Reilly Media, publishers of Java Generics and Collections, have approved a proposal for a second edition. This news came from Zan McQuade, Programming Languages acquisition editor at O’Reilly, whose strategies for acquiring content include indefatigable patience—she calmly waited out two years of silence from me after I first pitched the idea to her. In fact, I’ve been thinking about it for much longer than that, starting from when I was writing Mastering Lambdas in 2014. Despite the title, that book is mostly about Java streams, which since Java 8 have been complementary to collections for processing bulk data. A half-book on collections seemed to need revision from that point on.

But that’s only half the book, and—it’s always seemed to me—the less important half. After all, the original USP of JG&C was its co-authorship by Phil Wadler, who was one of the originators of the “Generic Java” prototype that eventually led to the introduction of generics in Java 5 in 2004. Nowadays few Java programmers will remember how controversial and sometimes difficult generics seemed at their introduction, and how important it was to have an authoritative explanation of their peculiarities. But those have changed little in nearly two decades, even if Project Valhalla seems likely to alter that considerably at some point in the relatively near future, for some value of “relatively near”. Perhaps in another year we’ll know enough about Valhalla to be able to change the generics half of the book in line with it—or perhaps Zan will have to go back in two years’ time to argue for a third edition!

But meanwhile the Java Collections Framework has continued to evolve—perhaps without huge changes, but with enough to justify a revision. Much of this evolution has been adaptation to Java’s journey in the direction of a more functional style. A prime example is unmodifiable collections; although it’s more than four years since they were introduced in Java 9, many people are only now migrating to Java 11—or, equally likely, to Java 17. If the latter, they will encounter records too, so this seems like a good time for an explanation of how these different functionally-oriented features, as well as streams, can work together.

Another reason for a new edition is the ageing of the Java Collections Framework. (Actually, in personal sympathy with this elderly API, I should probably say “maturing”.) It’s worn very well for an API designed in the last century. (On another personal note, I have to tip my hat here to Joshua Bloch, the designer of the JCF. At a time when I’d given up hope of ever getting the collections material into shape, he very generously provided an extraordinarily detailed, precise—and painful!—technical review, highlighting virtually every one of my many errors, and saving the collections material from disaster.) But JG&C was written at a time when the JCF was still only five years old. Nearly two decades on, we have the opportunity for a much more considered design retrospective and for a comparison with other collections frameworks, like Guava and Eclipse collections, that have appeared since then.

I’m also looking forward to supplying two other elements absent from the first edition. One is a cause of increasing dissatisfaction for me with the collections half of JG&C: its discussion of the relative performance of the different collection implementations. I compared them there solely on the basis of their asymptotic (Big-O) performance, without providing any experimental results. That’s quite embarrassing now, after I’ve given so many conference talks on the difficulty and importance of accurate measurement when discussing performance. And since a (half-)book on collections is one place where such discussions are inescapable, I’m looking forward to providing data to back up the theory—or, more likely, to require its modification to fit with modern machine architectures.

I’m feeling quite—perhaps foolishly—confident about this revision, very much in contrast to my feelings approaching the first edition. Much of that is due to already having a technical editor on board, the tireless Doctor Deprecator, Stuart Marks. Stuart is ideally placed for this, being the Oracle lead on the Java collections library. He was TE on Mastering Lambdas, so I’ve had the pleasure of working with him before, and he’s already provided a lot of the ideas in support of the book proposal, including some in this blog piece. If you’ve read this far (congratulations!) you’ll see there’s quite a lot of work to do, but with Stuart on the team I’m confident that we’re really going to produce something valuable for the working Java programmer.

Join the Conversation

2 Comments

  1. Thanks. It would be very helpful to have the second edition updated for new version of Java.

Leave a comment

Your email address will not be published. Required fields are marked *