Lecture 16: Case Study: The Java
Collections API
You can’t be a competent Java programmer without understanding the crucial parts of
the Java library. The basic types are all in
java.lang
, and are part of the language prop-
er. The package
java.util
provides collections – sets, lists and maps – and you should
know it well. The package
java.io
is also important, but you can get away with only a
rough familiarity with what’s in it, delving in as needed.
In this lecture, we’ll look at the design of java.util, often called the Java ‘collections API’.
It’s worth understanding not only because the collection classes are extremely useful,
but also because the API is a nice example of well-engineered code. It’s fairly easy to
understand, and is very well documented. It was designed and written by Joshua Bloch,
who wrote the
Effective Java
book that we recommended at the start of term.
At the same time, though, almost all the complexities of object-oriented programming
appear somewhere in it, so if you study the API carefully, you’ll get a broad under-
standing of programming issues that you probably haven’t yet considered in your own
code. In fact, it wouldn’t be an exaggeration to say that if you figure out how just one
of the classes (eg,
ArrayList
) works in its entirety, then you will have mastered all the
concepts of Java. We won’t have time to look at all the issues today, but we’ll touch on
many of them. Some of them, such as serialization and synchronization, are beyond the
scope of the course.
16.1
Type Hierarchy
Roughly, the API offers three kinds of collection: sets, lists and maps. A set is a collec-
tion of elements that does not maintain their order or their count – each element is
either in the set or not. A list is a sequence of elements, and thus maintains both order
and count. A map is an association between keys and values: it holds a set of keys, and
maps each key to a single value.
The API organizes its classes with a hierarchy of interfaces – the specifications of the
various types – and a separate hierarchy of implemention classes. The diagram shows
some select classes and interfaces to illustrate this. The interface
Collection
captures
the common properties of lists and sets, but not maps, but we’ll use the informal term
93

‘collections’ to refer to maps anyway.
SortedMap
and
SortedSet
are used for maps and
sets which provide additional operations to retrieve the elements in some order.
The concrete implementation classes, such as
LinkedList
, are built on top of skeletal
implementations, such as
AbstractList
, from which they inherit. This parallel structure
of interfaces and classes is an important idiom that is worth studying.
Many novice
94
extends
AbstractCollecti
on
Collection
Set
List
SortedSet
extends
AbstractSet
AbstractList
implements
implements
implements
implements
HashSet
TreeSet
extends
Abstract
SequentialList
LinkedList
interfaces
ArrayList
extends
extends
extends

programmers are tempted to use abstract classes when they should be using interfaces.
