Java Collections Framework: A Unified Approach to Data Management
Discover the evolution of Java's data management capabilities with the Collections Framework, introduced in Java 2. Before its inception, Java relied on ad-hoc classes like Dictionary
, Vector
, Stack
, and Properties
for storing and manipulating groups of objects. While these classes were functional, they lacked a cohesive structure. Learn how the Collections Framework provides a unified theme, simplifying the usage of various collection types and enhancing code efficiency in Java programming.
Java - Collections Framework
Prior to Java 2, Java provided ad-hoc classes like Dictionary
, Vector
, Stack
, and Properties
to store and manipulate groups of objects. Although these classes were useful, they lacked a central, unifying theme. For instance, the way you used Vector
was different from how you used Properties
.
Why Collections Framework?
The collections framework was designed to meet several goals:
- It needed to be high-performance, with efficient implementations for fundamental collections like dynamic arrays, linked lists, trees, and hash tables.
- It needed to allow different types of collections to work similarly and interoperate efficiently.
- It needed to be easy to extend or adapt.
The framework revolves around standard interfaces, and several standard implementations such as LinkedList
, HashSet
, and TreeSet
are provided. You can also implement your own collections if needed.
Java Collections Framework
The collections framework provides a unified architecture for representing and manipulating collections. All collections frameworks contain the following components:
1. Interfaces
Abstract data types that represent collections. Interfaces allow collections to be manipulated independently of their representation. In object-oriented languages, interfaces form a hierarchy.
2. Implementations (Classes)
Concrete implementations of the collection interfaces, serving as reusable data structures.
3. Algorithms
Methods that perform useful computations like searching and sorting on objects that implement collection interfaces. The algorithms are polymorphic, meaning the same method can be used on different implementations of the collection interface.
In addition to collections, the framework defines several map interfaces and classes. Maps store key/value pairs. Although not technically collections, they are fully integrated with the collections framework.
Hierarchy of Collection Framework
All classes and interfaces in the collections framework are available in the java.util
package. The following diagram shows the hierarchy of the collection framework in Java:
Java Collection Interfaces
The collections framework defines several interfaces. Here's an overview of each:
- Collection Interface: Enables you to work with groups of objects; it sits at the top of the collections hierarchy.
- List Interface: Extends
Collection
; stores an ordered collection of elements. - Set Interface: Extends
Collection
; handles sets, which must contain unique elements. - SortedSet Interface: Extends
Set
; handles sorted sets. - Map Interface: Maps unique keys to values.
- Map.Entry Interface: Describes a key/value pair in a map; this is an inner class of
Map
. - SortedMap Interface: Extends
Map
so keys are maintained in ascending order. - Enumeration Interface: A legacy interface that defines methods to enumerate elements in a collection. It has been superseded by
Iterator
.
Java Collection Classes
Java provides several standard collection classes that implement Collection
interfaces. Some provide full implementations, while others serve as abstract classes, providing skeletal implementations for creating concrete collections.
List of Standard Collection Classes
- AbstractCollection: Implements most of the
Collection
interface. - AbstractList: Extends
AbstractCollection
and implements most of theList
interface. - AbstractSequentialList: Extends
AbstractList
for collections that use sequential access. - LinkedList: Implements a linked list by extending
AbstractSequentialList
. - ArrayList: Implements a dynamic array by extending
AbstractList
. - AbstractSet: Extends
AbstractCollection
and implements most of theSet
interface. - HashSet: Extends
AbstractSet
for use with hash tables. - LinkedHashSet: Extends
HashSet
to allow insertion-order iteration. - TreeSet: Implements a set stored in a tree by extending
AbstractSet
. - AbstractMap: Implements most of the
Map
interface. - HashMap: Extends
AbstractMap
to use a hash table. - TreeMap: Extends
AbstractMap
to use a tree. - WeakHashMap: Extends
AbstractMap
to use a hash table with weak keys. - LinkedHashMap: Extends
HashMap
to allow insertion-order iterations. - IdentityHashMap: Extends
AbstractMap
and uses reference equality for comparison.
Legacy Collection Classes
These classes, defined in java.util
, include:
- Vector: Implements a dynamic array, similar to
ArrayList
but with differences. - Stack: A subclass of
Vector
that implements a last-in, first-out stack. - Dictionary: An abstract class representing a key/value storage, similar to
Map
. - Hashtable: A concrete implementation of
Dictionary
. - Properties: A subclass of
Hashtable
used to maintain lists of key/value pairs. - PriorityQueue: An unbounded priority queue based on a priority heap.
- BitSet: A class that creates an array holding bit values.
- ArrayDeque: Provides a resizable-array implementation of
Deque
. - EnumMap: A specialized
Map
implementation for use with enum keys. - Queue: Implements a FIFO (First In First Out) collection.
- Deque: Implements a double-ended queue.
Collection Algorithms
The collections framework defines several algorithms as static methods within the Collections
class. These include methods for sorting, searching, and other operations.
Be aware that some methods may throw ClassCastException
when attempting to compare incompatible types or UnsupportedOperationException
when trying to modify an unmodifiable collection.
How to Use an Iterator?
An Iterator
allows you to cycle through a collection, obtaining or removing elements. ListIterator
extends Iterator
to allow bidirectional traversal and modification of list elements.
How to Use a Comparator?
TreeSet
and TreeMap
store elements in sorted order, defined by a comparator. A Comparator
can be used to sort collections in various ways, even for classes you cannot modify.
How to Use a Comparable?
Similar to a Comparator
, the Comparable
interface defines the natural sorting order for objects.
Summary
The Java collections framework provides prepackaged data structures and algorithms for manipulating them. Collections are objects that hold references to other objects, and the framework's interfaces declare operations that can be performed on these collections.
All the collection-related classes and interfaces are in the java.util
package.