Java

Java Collections API Overview

Introduction

The Java 1.2 platform includes a new collections API. A collection (also known as a container) is a single object representing a group of objects (such as the familiar Vector class). A collections API is a unified framework for representing and manipulating collections, allowing them to be manipulated independent of the details of their representation.

The primary advantages of a Collections framework are:

The JDK Collections API consists of:

Design Goals

Our main design goal was to produce an API that was reasonably small, both in size, and (more importantly) in "conceptual weight." It was critical that the new functionality not seem alien to current Java programmers; it had to augment current facilities, rather than replacing them. At the same time, the new API had to be powerful enough to provide all the advantages described above.

To keep the number of core interfaces small, we did not attempt to capture subtle distinctions (such as mutability, modifiability, resizability) in the interface Hierarchy. Instead, we documented certain calls in the core interfaces as optional, allowing implementations to throw an UnsupportedOperationException to indicate that they do not support a specified optional operation. Of course, Collection implementers must clearly document which optional operations are supported by an implementation.

To keep the number of methods in each core interface small, we included a method only if:

  1. it was a truly "fundamental operation" (a basic operations in terms of which others could be reasonably defined), or
  2. there was a compelling performance reason why an important implementation would want to override it.

It was critical that all reasonable representations of collections interoperate well. This includes arrays, which cannot be made to implement Collection directly without changing the language. Thus, we provided methods to allow Collections to be dumped into arrays, arrays to be viewed as Collections, and Maps to be viewed as Collections.


Copyright © 1995-97 Sun Microsystems, Inc. All Rights Reserved.



Please send comments to: collections-comments@java.sun.com
Sun
Java Software