|
|
Contents |
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 ofCollection, and aSortedSetis a special kind ofSet, and so forth. Note also that the hierarchy consists of two distinct trees: aMapis not a trueCollection.To keep the number of core collection interfaces managable, the 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.
Collection
The Collectioninterface is the root of the collection hierarchy. A
Collectionrepresents a group of objects, known as its elements. SomeCollectionimplementations 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 likeSetandList. 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.Set
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.
List
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 theListeach 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.Map
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 (
SortedSetandSortedMap) are merely sorted versions ofSetandMap. In order to understand these interfaces, you have to know how order is maintained among objects. Even if you don't plan to useSortedSetorSortedMap, read the following section if you plan to sortLists.Object Ordering
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:
SortedSet
ASortedSetis aSetthat maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. TheSortedSetinterface is used for things like word lists and membership rolls.SortedMap
A SortedMapis a
Mapthat maintains its mappings in ascending key order. It is theMapanalogue ofSortedSet. TheSortedMapinterface is used for apps like dictionaries and telephone directories.
|
|
Contents |