The Java Collections Framework – Collections: Part II
15.1 The Java Collections Framework
A collection allows a group of objects to be treated as a single entity. Objects can be stored, retrieved, and manipulated as elements of a collection. Arrays are an example of one kind of collection.
Program design often requires the handling of collections of objects. The Java Collections Framework provides a set of standard utility classes for managing various kinds of collections. The core 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 15.1 and Table 15.1). These generic interfaces define the common functionality exhibited by collections, and facilitate data exchange between collections.
Figure 15.1 The Core Interfaces
Table 15.1 Core Interfaces and Concrete Classes in the Collections Framework
Interface | Description | Concrete classes |
Collection<E> | A basic interface that defines the normal operations that allow a collection of objects to be maintained or handled as a single unit. | – |
List<E> extends Collection<E> | The List<E> interface extends the Collection<E> interface to maintain a sequence of elements that can contain duplicates. | ArrayList<E>, Vector<E>, LinkedList<E> |
Set<E> extends Collection<E> | The Set<E> interface extends the Collection<E> interface to represent its mathematical namesake: a set of unique elements. | HashSet<E>, LinkedHashSet<E> |
SortedSet<E> extends Set<E> | The SortedSet<E> interface extends the Set<E> interface to provide the required functionality for maintaining a set in which the elements are stored in some sort order. | TreeSet<E> |
NavigableSet<E> extends SortedSet<E> | The NavigableSet<E> interface extends and replaces the SortedSet<E> interface to maintain a sorted set, and should be the preferred choice in new code. | TreeSet<E> |
Queue<E> extends Collection<E> | The Queue<E> interface extends the Collection<E> need to be processed according to some policy—that is, insertion at one end and removal at the other, usually as FIFO (first in, first out). | PriorityQueue<E>, LinkedList<E> |
Deque<E> extends Queue<E> | The Deque<E> interface extends the Queue<E> interface to maintain a queue whose elements can be processed at both ends: double-ended queue. | ArrayDeque<E>, LinkedList<E> |
Map<K,V> | A basic interface that defines operations for maintaining mappings of keys to values. | HashMap<K,V>, Hashtable<K,V>, LinkedHashMap<K,V> |
SortedMap<K,V> extends Map<K,V> | The SortedMap<K, V> interface extends the Map<K, V> interface for maps that maintain their mappings sorted in key order. | TreeMap<K,V> |
NavigableMap<K,V> extends SortedMap<K,V> | The NavigableMap<K, V> interface extends and replaces the SortedMap<K, V> interface for sorted maps. | TreeMap<K,V> |
- A set of implementations (i.e., concrete classes, listed in Table 15.1) that are specific implementations of the core interfaces, providing data structures that a program can readily use.
- Algorithms that are an assortment of static utility methods found in the Collections and Arrays classes that can be used to perform various operations on collections and arrays, such as sorting and searching, or creating customized collections (§15.11, p. 856, and §15.12, p. 864).