The Java Collections Framework (JCF) is a set of classes and interfaces that implement commonly reusable data structures.
It provides a unified architecture for storing and manipulating groups of objects. Before the JCF, Java had several ad-hoc classes like Array, Vector, and Hashtable, but they didn't share a common interface, making them difficult to use together. The JCF fixed this by providing a standardized set of tools.
The framework is built around a set of core interfaces. The main ones are:
!Collections Framework Diagram
Collection Interface: The root of the hierarchy. It represents a group of objects, known as its elements. It defines basic methods like add(), remove(), size(), and iterator().
Set Interface: Extends Collection. A collection that cannot contain duplicate elements. Think of a mathematical set.HashSet, TreeSet, LinkedHashSet.List Interface: Extends Collection. An ordered collection (also known as a sequence). Lists can contain duplicate elements. You can access elements by their integer index.ArrayList, LinkedList.Queue Interface: Extends Collection. A collection used to hold elements prior to processing. Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations. Typically, queues order elements in a FIFO (first-in-first-out) manner.LinkedList, PriorityQueue.Map Interface: This interface is not a true Collection because it doesn't extend the Collection interface, but it's a core part of the framework. It maps unique keys to values.
HashMap, TreeMap, LinkedHashMap.Here's a quick look at the most frequently used collection classes:
| Interface | Implementation | Description |
|---|---|---|
List |
ArrayList |
A resizable array. Fast for random access (get()), but slower for insertions and deletions in the middle. |
List |
LinkedList |
A doubly-linked list. Fast for insertions and deletions, but slower for random access. |
Set |
HashSet |
Stores elements in a hash table. Provides the best performance on average (O(1)), but makes no guarantees about iteration order. |
Set |
TreeSet |
Stores elements in a sorted tree structure. Elements are ordered, but performance is slower (O(log n)). |
Set |
LinkedHashSet |
A hash table and linked list implementation. Provides insertion-ordered iteration and fast (O(1)) performance. |
Map |
HashMap |
A hash table implementation of the Map interface. Provides O(1) performance for get() and put(). No order is guaranteed. |
Map |
TreeMap |
A tree-based implementation. Keys are sorted, but performance is O(log n). |
Map |
LinkedHashMap |
A hash table and linked list implementation. Provides insertion-ordered iteration and fast (O(1)) performance. |
In the next chapters, we will explore each of these interfaces and implementations in detail.