|
|
Start of Tutorial > Start of Trail |
Search
Feedback Form |
The core collection interfaces encapsulate different types of collections and are shown in the figure below. These interfaces allow collections to be manipulated independently of the details of their representation. The core collection interfaces are the foundation of the Java Collections Framework.As you can see from the figure, the core collection interfaces form a hierarchy: a Setis a special kind ofCollection, aSortedSetis a special kind ofSet, and so forth. Note also that the hierarchy consists of two distinct trees: aMapis not a trueCollection.Note that all of the core collection interfaces are generic. For example, the declaration of the
Collectioninterface is:The <E> syntax tells you that the interface is generic. When you declare a <code>public interface Collection<E> ...Collectioninstance you can and should specify the type of object contained in the collection. Specifying the type allows the compiler to verify (at compile time) that the type of object you put into the collection is correct, thus reducing errors at runtime. For information on generic types, see the section Generics.
When you understand how to use these interfaces, you know most of what there is to know about the Collections Framework. This chapter will discuss general guidelines for effective use of these interfaces, including when to use which interface. You'll also learn programming idioms for each interface to help you get the most out of it.
To keep the number of core collection interfaces manageable, the Java platform doesn't provide separate interfaces for each variant of each collection type. (Such variants might include immutable, fixed-size, and append-only.) Instead, the modification operations in each interface are designated optional: a given implementation may elect not to support all 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 Java platform's general-purpose implementations support all the optional operations.
The core collection interfaces are:
The last two core collection interfaces are merely sorted versions of
Collection: The root of the collection hierarchy. A collection represents a group of objects, known as its elements. TheCollectioninterface is the least common denominator that all collections implement, and is used to pass collections around and to manipulate them when maximum generality is desired. Some types of collections allow duplicate elements, and others do not. Some are ordered and others unordered. The Java platform doesn't provide any direct implementations of this interface but provides implementations of more specific subinterfaces, such asSetandList. See the section The Collection Interface.
Set: A collection that cannot contain duplicate elements. This interface models the mathematical set abstraction and is used to represent sets, such as the cards comprising a poker hand, the courses making up a student's schedule, or the processes running on a machine. See the section The Set Interface.
List: An ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of aListgenerally has precise control over where in theListeach element is inserted, and can access elements by their integer index (position). If you've usedVector, you're familiar with the general flavor ofList. See the section The List Interface.
Queue: A collection used to hold multiple elements prior to processing. Besides basicCollectionoperations, queues provide additional insertion, extraction, and inspection operations.Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator, or the elements' natural ordering. Whatever the ordering used, the head of the queue is that element that would be removed by a call to
removeorpoll. In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. EveryQueueimplementation must specify its ordering properties. See the section The Queue Interface.
Map: An object that maps keys to values. Maps cannot contain duplicate keys: Each key can map to at most one value. If you've usedHashtable, you're already familiar with the general flavor ofMap. See the section The Map Interface.
SetandMap:To understand how the sorted interfaces maintain the order of their elements, see Object Ordering
SortedSet: A Set that maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls. See the section The SortedSet Interface.
SortedMap: AMapthat maintains its mappings in ascending key order. This is theMapanalog ofSortedSet. Sorted maps are used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories. See the section The SortedMap Interface.
.
|
|
Start of Tutorial > Start of Trail |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.