Ever since the release of Java 5, I've been keeping my eyes open for a book that describes what I believe to be the most powerful new feature of Java - generics. The new book published by O'Reilly, Java Generics and Collections covers the topic in spades. As a bonus, the second half of this book examines the Java Collections Framework. Since the Collections Framework was rewritten to incorporate the use of generics, it makes perfect sense for the authors to spend a reasonable amount of time describing the new interfaces.
A brief introduction highlights the use of generics, autoboxing, foreach loops, generic methods and varargs. The examples are written using the Collections Framework. Subtyping and the use of wildcards follow the introduction and begin to show the real power of generics.
Getting past the basics, a chapter devoted to the Comparable and Comparator interfaces describes some very useful concepts, such as how to use the Comparable interface to find the minimum or maximum element in a collection. The use of enumerated types is briefly covered here, as well as the concepts of multiple bounds, bridges and covariant overriding.
A chapter describing how to declare a generic class helps you build your own classes. Following that is a useful chapter that describes how the design of generics is evolutionary, not revolutionary. It details how generics in Java 5 allow you to gradually migrate your legacy code, rather than replace it all at once.
If you use generic types in casts, instance testing, exceptions or arrays, a chapter on reification warns you of rough edges and limitations you may find and describes some workarounds. How generics changes reflection is also covered.
The last two chapters of the first half of the book describe how to use generics effectively in your code and provide samples of how implementations of select design patterns can take advantage of generics.
The second half of the book, which dives into the Collections Framework, starts with a quick chapter called 'Preliminaries' that spends time discussing underlying concepts, such as the Iterable interface. Thread safety issues are covered in this chapter as well. Thread-safe concurrent collections were introduced in Java 5 and their use, as compared with synchronized collections is also discussed.
The Collection interface is given its own chapter, which is important considering that it defines functionality common to any type of collection other than maps. Following are chapters describing sets, queues and lists. The Map interface also warrants its own chapter, in which the available methods and the various map implementations are discussed. The comparative performance of different set, queue, list and map implementations is shown in tables at the end of each chapter.
Following the Collection and Map interface chapters is the final chapter which describes the Collections class in detail. The Collections class contains static methods that operate on collections or return them.
I found the code and diagrams in this book very easy to understand and a great resource when trying to decide which collection or map implementation to use for a specific problem.
Once you learn how to take advantage of generics, you'll begin to write more useful, reusable code. This book now occupies a space on my shelf next to my Algorithms and Data Structures books.
|