|Start of Tutorial > Start of Trail||
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
Setis a special kind of
Collection, and a
SortedSetis a special kind of
Set, and so forth. Note also that the hierarchy consists of two distinct trees: a
Mapis not a true
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
Collectionrepresents a group of objects, known as its elements. Some
Collectionimplementations 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
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
Listgenerally has precise control over where in the
Listeach 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
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
The last two core collection interfaces (
SortedMap) are merely sorted versions of
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
SortedMap, read the
following section if you plan to sort
There are two ways to order objects: The Comparableinterface provides automatic natural order on classes that implement it, while the Comparatorinterface 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:
Setthat maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. The
SortedSetinterface is used for things like word lists and membership rolls.
A SortedMapis a
Mapthat maintains its mappings in ascending key order. It is the
SortedMapinterface is used for apps like dictionaries and telephone directories.
|Start of Tutorial > Start of Trail||
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.