11.1 The Collections Framework

A collection allows a group of objects to be treated as a single unit. Arbitrary objects can be stored, retrieved, and manipulated as elements of collections.

Program design often requires handling of groups of objects. The collections framework presents a set of standard utility classes for managing such collections. This framework is provided in the java.util package and comprises three main parts:

  • The core interfaces that allow collections to be manipulated independently of their implementation (see Figure 11.1 and Table 11.1). These interfaces define the common functionality exhibited by collections and facilitate data exchange between collections.

    Figure 11.1. The Core Interfaces

    graphics/11fig01.gif

  • A small set of implementations (i.e., concrete classes, listed in Table 11.1) that are specific implementations of the core interfaces, providing data structures that a program can use readily.

  • An assortment of static utility methods that can be used to perform various operations on collections, such as sorting and searching, or creating customized collections.

Core Interfaces

The Collection interface is a generalized interface for maintaining collections, and is the top of the interface inheritance hierarchy for collections shown in Figure 11.1a. These interfaces are summarized in Table 11.1.

Table 11.1. Core Interfaces in the Collections Framework

Interface

Description

Concrete Classes

Collection

A basic interface that defines the normal operations that allow a collection of objects to be maintained or handled as a single unit.

 

Set

The Set interface extends the Collection interface to represent its mathematical namesake: a set of unique elements.

HashSet
LinkedHashSet

SortedSet

The SortedSet interface extends the Set interface to provide the required functionality for maintaining a set in which the elements are stored in some sorted order.

TreeSet

List

The List interface extends the Collection interface to maintain a sequence of elements that need not be unique.

ArrayList
Vector
LinkedList

Map

A basic interface that defines operations for maintaining mappings of keys to values.

HashMap
Hashtable
LinkedHashMap

SortedMap

Extends the Map interface for maps that maintain their mappings sorted in key order.

TreeMap

The elements in a Set must be unique, that is, no two elements in the set can be equal. The order of elements in a List is retained, and individual elements can be accessed according to their position in the list.

As can be seen from Figure 11.1b, the Map interface does not extend the Collection interface because conceptually, a map is not a collection. A map does not contain elements. It contains mappings (also called entries) from a set of key objects to a set of value objects. A key can, at most, be associated with one value. As the name implies, the SortedMap interface extends the Map interface to maintain its mappings sorted in key order.

Implementations

The java.util package provides implementations of a selection of well-known abstract data types, based on the core interfaces. Figures 11.2 and 11.3 show the inheritance relationship between the core interfaces and the corresponding implementations. None of the concrete implementations inherit directly from the Collection interface. The abstract classes provide the basis on which concrete classes are implemented.

Figure 11.2. The Core Collection Interfaces and Their Implementations

graphics/11fig02.gif

Figure 11.3. The Core Map Interfaces and Their Implementations

graphics/11fig03.gif

By convention, each of the collection implementation classes provides a constructor for creating a collection based on the elements of another Collection object passed as argument. This allows the implementation of a collection to be changed by merely passing the collection to the constructor of the desired implementation. This interchangeability is also true between Map implementations. But collections and maps are not interchangeable. Note that a collection (or a map) only stores references to objects, and not the actual objects.

The collections framework is interface-based, meaning that collections are manipulated according to their interface types, rather than by the implementation types. By using these interfaces wherever collections of objects are used, various implementations can be used interchangeably.

All the concrete classes shown in Figures 11.2 and 11.3 implement the Serializable and the Cloneable interfaces; therefore, the objects of these classes can be serialized and also cloned.

A summary of collection and map implementations is given in Table 11.2. The contents of this table will be the focus as each core interface and its corresponding implementations are discussed in the following sections.

Table 11.2. Summary of Collection and Map Implementations

Concrete Collection/Map

Interface

Duplicates

Ordered/Sorted

Methods Called on Elements

Data Structures on Which Implementation Is Based

HashSet

Set

Unique elements

No order

equals()
hashCode()

Hash table

LinkedHashSet

Set

Unique elements

Insertion order

equals()
hashCode()

Hash table and doubly-linked list

TreeSet

SortedSet

Unique elements

Sorted

equals()
compareTo()

Balanced tree

ArrayList

List

Allowed

Insertion order

equals()

Resizable array

LinkedList

List

Allowed

Insertion order

equals()

Linked list

Vector

List

Allowed

Insertion order

equals()

Resizable array

HashMap

Map

Unique keys

No order

equals()
hashCode()

Hash table

LinkedHashMap

Map

Unique keys

Key insertion order/Access order of entries

equals()
hashCode()

Hash table and doubly-linked list

Hashtable

Map

Unique keys

No order

equals()
hashCode()

Hash table

TreeMap

SortedMap

Unique keys

Sorted in key order

equals()
compareTo()

Balanced tree