The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail Search
Feedback Form

Trail: Collections

Lesson: Interfaces

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.

The Core Collection Interfaces

As you can see from the figure, the core collection interfaces form a hierarchy: a Set is a special kind of Collection, 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.

Note that all of the core collection interfaces are generic. For example, the declaration of the Collection interface is:

public interface Collection<E> ...
The <E> syntax tells you that the interface is generic. When you declare a <code> Collection instance 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 (in the Collections trail).

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 (in the API reference documentation). 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 Set and Map: To understand how the sorted interfaces maintain the order of their elements, see Object Ordering (in the Collections trail).

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.