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.
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.
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.
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.
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.
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.