|
Java Collections API Overview
|
Introduction
The Java 1.2 platform includes a new collections API.
A collection (also known as a
container) is a single object representing a group of objects
(such as the familiar
Vector class).
A collections API is a unified framework for representing and manipulating
collections, allowing them to be manipulated independent
of the details of their representation.
The primary advantages of a Collections framework are:
- It provides interoperability between unrelated APIs. If
my SQL database API furnishes a Collection and your GUI toolkit expects
a Collection, our APIs will interoperate seamlessly even though they
were written independently.
- It reduces the effort required to learn APIs.
If my database API returns a List backed
by the database, your GUI toolkit returns a List that represents widgets on
a screen, and I know how to manipulate the database-backed List, then I also
know how to manipulate the widget List.
- It reduces the effort required to design and implement
APIs.Designers and implementers don't have to "reinvent the wheel"
each time they create an API that relies on collections.
- It fosters software reuse, by allowing programmers to
implement generic, reusable data objects that conform to the standard
Collection interfaces, and to implement algorithms that operate on
Collection objects independent of the details of their representation
(polymorphism).
The JDK Collections API consists of:
- Collection Hierarchy - Six core collection
interfaces representing different types of collections (such as sets,
lists, and maps).
- Concrete Implementations - General-purpose
implementations of the core collection interfaces.
- Anonymous Implementations - Several static factory
methods that return Collections. For example, one of these returns a
List backed by a specified array, which allows legacy APIs that return
arrays to interoperate with new APIs that expect Collections.
- Abstract Implementations - Partial implementations of
the core collection interfaces, to facilitate custom implementations.
- Infrastructure - Several new interfaces used in defining
the Collection Hierarchy, most notably, two new iterators, and two interfaces
that define element ordering.
- Algorithms - Several static methods that perform
useful functions polymorphically on an object that implements an
appropriate collection interface (e.g., sorting a list).
- Array Sorting and Searching - Not really a part of the
Collections API, but this functionality is being added to the JDK at the
same time, and relies on the same infrastructure.
Design Goals
Our main design goal was to produce an API that was reasonably small,
both in size, and (more importantly) in "conceptual weight." It was
critical that the new functionality not seem alien to current Java
programmers; it had to augment current facilities, rather than replacing
them. At the same time, the new API had to be powerful enough to provide
all the advantages described above.
To keep the number of core interfaces small, we did not attempt to
capture subtle distinctions (such as mutability, modifiability, resizability)
in the interface Hierarchy. Instead, we documented certain calls in the
core interfaces as optional, allowing implementations to throw an
UnsupportedOperationException to indicate that they do not support a specified
optional operation. Of course, Collection implementers must clearly
document which optional operations are supported by an implementation.
To keep the number of methods in each core interface small, we
included a method only if:
- it was a truly "fundamental operation" (a basic operations in terms of
which others could be reasonably defined), or
- there was a compelling performance reason why an important
implementation would want to override it.
It was critical that all reasonable representations of collections
interoperate well. This includes arrays, which cannot be made to implement
Collection directly without changing the language. Thus, we provided
methods to allow Collections to be dumped into arrays, arrays to be viewed
as Collections, and Maps to be viewed as Collections.