About the Reviewer
Aditya Gore has a Bachelor's degree in Computer Engineering. He has more than five years of experience in software development, almost all of it in Java. He currently works for NDS India in Bangalore.
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.

Java Generics And Collections

Author(s) Maurice Naftalin, Philips Wadler
Publisher O'Reilly
PubDate 2006, Edition 1
Reviewer Aditya Gore



One Minute Review


Positivies
Negatives
  • Great coverage of Java 5 Generics
  • Excellent flow
  • Pithy examples
  • Could have done with more sample code

Sections

Intent & Audience

This book is designed as a tutorial for Generics in Java 5 and as a guide for using the Collections framework effectively with Generics. The audience would be Java developers with prior experience using Java pre-Generics. For those interested in Generics alone, reading chapters 1 through 9 should suffice. But for more advanced developers, Part II of the book that deals with the Collections framework is a must read. In my opinion, after reading this book you should be able to use Generics and the new Collections framework with confidence.
(For more details about some wonderful nuggets that you are likely to encounter while reading this book, please see the last link in the resources section - a review of this book by Dr. Heinz M. Kabutz of JavaSpecialists fame)

Relevance of material

Part I of the book covers Generics introduced in Java 5 and Part II is all about using the techniques learnt in Part I with the Collection framework (re-engineered to make use of Generics). However, this book should not be treated as an overall Java 5 language guide since its focus is quite narrow.

Chapter highlights

The book is divided into 2 parts. Chapters 1 through 9 constitute Part I and chapters 10 through 17 constitute Part II.

Part I : Generics

Chapter 1: Introduction
This chapter introduces Generics and the new language features in Java 5, viz. boxing/unboxing, the new foreach loop and varargs. Examples involving the Collection framework illustrate how all these new features make it easiser to write java code.

Chapter 2: Subtyping and Wildcards
This chapter discusses how subtyping works and how wildcards facilitate subtyping in connection with Generics. Two important principles - the so called "Subtyping Principle" and the "Get and Put principle" - that govern subtyping and wildcards respectively are introduced here. The chapter ends with an overview of wildcard capture and restrictions while using wildcards.

Chapter 3: Comparison and Bounds
This chapter describes the Comparable<T> and Comparator<T> interfaces. These interfaces support element comparison. Also described are enumerated types (java.lang.Enum) and type variable bounds. The chapter ends with a discussion on covariant return types, a new feature introduced by Java 5.

Chapter 4: Declarations
This chapter describes in detail the declaration of generic classes, constructors, static members and nested classes. It also provides a very readable, albeit brief, explanation of the mechanics of erasure.

Chapter 5: Evolution, Not Revolution
Java 5's design goal was to provide migration (or platform) compatibility rather than plain backward compatibility. The chapter illustrates this goal by providing code examples to add Generics to existing code. Starting with a legacy library and client, it provides code samples of all possible evolutionary permutations - generic library with legacy client, legacy library with generic client and finally generic library with generic client. The authors have a coined a cool motto as well - "erasure eases evolution".

Chapter 6: Reification
Reification is possibly the most complex concept to digest in Java 5 (at least in my opinion). This chapter begins with a precise definition of reification followed by discussions about instance tests, casts, exceptions and arrays in the context of reification. The chapter then describes 2 principles - "Principle of Truth in Advertising" and "Principle of Indecent Exposure" - that can help avoid the worst pitfalls in reification. The chapter goes on to describe the dangers associated with using varargs and wraps up by (slightly controversially) arguing that arrays ought to be deprecated in favour of Collections.

Chapter 7: Reflection
In this chapter the authors describe the changes in the reflection API introduced by Generics in two broad sections. Section 1, Generics for reflection, is a discussion about some of the types used for reflection (e.g Class) becoming generic (Class <T>). Section 2, Reflection for Generics, is a discussion about the changes in the API for reflecting upon generic types.

Chapter 8: Effective Generics
This chapter contains guidelines on using Generics effectively in practice. The following broad guidelines make up this chapter: Use checked Collections when calling generic code and to enforce security, Specialize to create reifiable types, Maintain binary compatibility while evolving legacy code.

Chapter 9: Design Patterns
This chapter describes how 5 well known design patterns - Visitor, Interpreter, Function, Strategy and Observer-Observable - can take advantage of Generics.

Part II : Collections

Chapter 10: The Main Interfaces of the Java Collections Framework
This two-page chapter is a very very quick tour of the Collections framework and introduces all the interfaces of the Collections framework.

Chapter 11: Preliminaries
Broad themes that form the underpinning of the entire Collections framework - the Iterator<E> and Iterable<T> interfaces, algorithmic efficiency and the 'Big O' notation, contracts defined by various Collection interfaces and Collections and thread-safety - are discussed here.

Chapter 12: The Collection Interface
As its name suggests, this chapter provides a high-level overview of the various contracts defined by the java.util.Collection interface.

Chapter 13: Set
This chapter is a tour of the java.util.Set interface, its concrete implementations (EnumSet, HashSet, LinkedHashSet, CopyOnWriteArraySet), its subinterfaces (SortedSet, NavigableSet) and their concrete implementations (TreeSet, ConcurrentSkipListSet). The chapter wraps up with a quick comparison of the various Set implementations.

Chapter 14: Queues
This chapter introduces the new java.util.Queue interface and its implementations (PriorityQueue, ConcurrentLinkedQueue), its subinterface BlockingQueue and its implementations (ArrayBlockingQueue, PriorityBlockingQueue, SynchronousQueue, DelayQueue, LinkedBlockingQueue). The chapter also discusses the java.util.Deque and implementations (LinkedList, ArrayDeque) and its subinterface BlockingDeque and its implementation LinkedBlockingDeque. This chapter ends with a comparison of various Queue implementations.

Chapter 15: Lists
This chapter discusses the java.util.List interface and its implementations (ArrayList, CopyOnWriteArrayList, LinkedList). Like the previous chapters this one too ends with a comparison of various list implementations.

Chapter 16: Maps
This chapter introduces the java.util.Map interface (which, incidentally happens to not derive from java.util.Collection). The discussion revolves around Map and its implementations (WeakHashMap, IdentityHashMap, EnumMap, HashMap, LinkedHashMap) its subinterfaces SortedMap, NavigableMap, ConcurrentMap, ConcurrentNavigableMap and the concrete implementations of these. Predictably, the chapter compares the various Map implementations at the end.

Chapter 17: The Collections Class
This chapter is an overview of the java.util.Collections class, which is a class full of static methods that operate on or return Collections. The most important methods in this class are briefly discussed here.

Rating

This book is a real gem. Wonderfully written and well illustrated with meaningful (although quite terse) examples it's a delightful read. Java programmers wishing to utilize the full power of Generics and the additions/modifications to the Collection framework should read this book. Though the new language features introduced by Java 5 are quite complex and vast and almost impossible to cover within a single book, this 254 page book takes a crack at attempting the impossible. Go out and grab a copy!!

Relevance
Readability
Overall

Resources

This book's website
Sample code from the book
Generics tutorial from Sun Microsystems (PDF)
Another review of this book by Dr. Heinz M. Kabutz

Purchase Java Generics And Collections on Amazon and help Javalobby.