A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve and manipulate data, and to transmit data from one method to another. Collections typically represent data items that form a natural group, like a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a collection of name-to-phone-number mappings).
If you've used Java -- or just about any other programming language --
you're already familiar with collections. Collection implementations in earlier
versions of Java included Vector
, Hashtable
, and array.
While earlier versions of Java contained collection implementations, they did
not contain a collections framework.
A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain three things:
The best known examples of collections frameworks are the
C++ Standard Template Library
(STL), and Smalltalk's collection classes.
Collection
of node names, and your GUI toolkit
expects a Collection of column headings, our APIs will interoperate
seamlessly even though they were written independently. Historically, collections frameworks have been quite complex, which gave them a reputation for having a steep learning curve. We believe that Java's new collections framework breaks with this tradition, as you will learn for yourself in the following lessons.
The core collection interfaces are the interfaces used to manipulate collections, and to pass them from one method to another. The basic purpose of these interfaces is to allow collections to be manipulated independently of the details of their representation. The core collection interfaces are the heart and soul of the collections framework. When you understand how to use these interfaces, you know most of what there is to know about the framework. The core collections interfaces are shown below:
The core collection interfaces form a hierarchy: a Set
is a special kind of Collection
, and a SortedSet
is a special kind of Set
, and so forth. Note also that the
hierarchy consists of two distinct trees: a Map
is not a true Collection
.
To keep the number of core collection interfaces manageable, the JDK doesn't
provide separate interfaces for each variant of each collection type. (Among
the possible variants are immutable, fixed-size, and
append-only.) Instead, the modification operations in each interface are
designated optional: a given implementation may not support some of
these operations. If an unsupported operation is invoked, a collection throws
an UnsupportedOperationException
. Implementations are responsible for documenting which of the optional
operations they support. All of the JDK's general
purpose implementations support all of the optional operations.
The four sections that follow teach you how to use each of the four basic core collection interfaces. In particular, they describe the idioms that allow you to use these interfaces effectively.
The Collection
interface is the root of the collection hierarchy. A
Collection
represents a group of
objects, known as its elements. Some Collection
implementations allow duplicate elements and others do not. Some are ordered
and others unordered. The JDK doesn't provide any direct implementations of
this interface: It provides implementations of more specific subinterfaces like Set
and List
. This interface is
the least common denominator that all collections implement. Collection is used
to pass collections around and manipulate them when maximum generality is
desired.
A Setis
a collection that cannot contain duplicate elements. As you might expect, this
interface models the mathematical set abstraction. It is used to
represent sets like the cards comprising a poker hand, the courses making up a
student's schedule, or the processes running on a machine.
A Listis
an ordered collection (sometimes called a sequence). Lists can contain
duplicate elements. The user of a
List
generally has precise control over where in the List
each element is inserted. The user can access
elements by their integer index (position). If you've used Vector,
you're already familiar with the general flavor of
List
.
A Mapis
an object that maps keys to values. Maps cannot contain duplicate keys: Each
key can map to at most one value. If you've used Hashtable
,
you're already familiar with the general flavor of
Map
.
The last two core collection interfaces (SortedSet
and SortedMap
) are merely sorted
versions of Set
and Map
. In order to understand these
interfaces, you have to know how order is maintained among objects. Even if you
don't plan to use SortedSet
or SortedMap
,
read the following section if you plan to sort List
s.
There are two ways to order objects: The Comparableinterface
provides automatic natural order on classes that implement it, while the
Comparator
interface
gives the programmer complete control over object ordering. Note that these are
not core collection interfaces, but underlying infrastructure.
Now that you know all about object ordering, here are the last two core collection interfaces:
A SortedSet
is a Set
that maintains its
elements in ascending order. Several additional operations are provided to take
advantage of the ordering. The SortedSet
interface is used for things like word
lists and membership rolls.
A SortedMapis
a
Map
that maintains its
mappings in ascending key order. It is the Map
analogue of SortedSet
.
The SortedMap
interface is used for apps like dictionaries and telephone directories.