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

  • 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
InterfaceDescriptionConcrete Classes
CollectionA basic interface that defines the normal operations that allow a collection of objects to be maintained or handled as a single unit. 
SetThe Set interface extends the Collection interface to represent its mathematical namesake: a set of unique elements.
HashSet
LinkedHashSet

SortedSetThe 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
ListThe List interface extends the Collection interface to maintain a sequence of elements that need not be unique.
ArrayList
Vector
LinkedList

MapA basic interface that defines operations for maintaining mappings of keys to values.
HashMap
Hashtable
LinkedHashMap

SortedMapExtends 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


Figure 11.3. The Core Map Interfaces and Their Implementations


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/MapInterfaceDuplicatesOrdered/SortedMethods Called on ElementsData Structures on Which Implementation Is Based
HashSetSetUnique elementsNo order
equals()
hashCode()

Hash table
LinkedHashSetSetUnique elementsInsertion order
equals()
hashCode()

Hash table and doubly-linked list
TreeSetSortedSetUnique elementsSorted
equals()
compareTo()

Balanced tree
ArrayListListAllowedInsertion orderequals()Resizable array
LinkedListListAllowedInsertion orderequals()Linked list
VectorListAllowedInsertion orderequals()Resizable array
HashMapMapUnique keysNo order
equals()
hashCode()

Hash table
LinkedHashMapMapUnique keysKey insertion order/Access order of entries
equals()
hashCode()

Hash table and doubly-linked list
HashtableMapUnique keysNo order
equals()
hashCode()

Hash table
TreeMapSortedMapUnique keysSorted in key order
equals()
compareTo()

Balanced tree

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.199.122