Start of Tutorial > Start of Trail |
Search
Feedback Form |
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 ofCollection
, and aSortedSet
is a special kind ofSet
, and so forth. Note also that the hierarchy consists of two distinct trees: aMap
is not a trueCollection
.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. ACollection
represents a group of objects, known as its elements. SomeCollection
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 likeSet
andList
. 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 aList
generally has precise control over where in theList
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 ofList
.
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 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:
ASortedSet
is aSet
that maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. TheSortedSet
interface is used for things like word lists and membership rolls.
A SortedMapis aMap
that maintains its mappings in ascending key order. It is theMap
analogue ofSortedSet
. TheSortedMap
interface is used for apps like dictionaries and telephone directories.
Start of Tutorial > Start of Trail |
Search
Feedback Form |
Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.