Fundamentals 31 min read

Comprehensive Overview of the Java Collection Framework: Interfaces, Implementations, and Usage

This article provides a detailed walkthrough of Java's collection framework, covering the hierarchy of interfaces and abstract classes, the characteristics of List, Set, and Map implementations, iterator mechanisms, common pitfalls, and practical code examples to help developers choose and use the appropriate collection types effectively.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Comprehensive Overview of the Java Collection Framework: Interfaces, Implementations, and Usage

1. Collection Framework Diagram

The Java collection classes are all located under the java.util package. The framework is built on two root interfaces, Collection and Map , each having several sub‑interfaces and concrete classes.

Simplified diagram:

Key points:

All collection classes reside in java.util .

Six collection interfaces (shown as short dashed lines) form the foundation.

Five abstract classes (long dashed lines) provide partial implementations.

Eight concrete classes (solid lines) are the actual implementations.

Collection allows duplicate elements; Set does not.

List maintains insertion order and permits duplicates.

Map stores key‑value pairs and does not extend Collection .

2. Overall Analysis

The core of the framework consists of Collection and Map . Collection is a highly abstract interface that defines basic operations and is split into List and Set sub‑interfaces.

List Interface

A List is an ordered collection where each element has an index. Implementations include ArrayList , LinkedList , Vector , and Stack .

(1) ArrayList

Dynamic array, default capacity 10, expands automatically. Adding n elements runs in O(n) amortized time. It excels at random access but is not synchronized.

size, isEmpty, get, set, iterator, listIterator – O(1)
add – amortized O(1)

(2) LinkedList

Implements a doubly‑linked list. Provides constant‑time insertions/removals at the ends but does not support fast random access. Also non‑synchronized; external synchronization is required for concurrent use.

List list = Collections.synchronizedList(new LinkedList(...));

(3) Vector

Synchronized dynamic array; thread‑safe but slower than ArrayList . API is almost identical to ArrayList .

(4) Stack

Extends Vector to provide LIFO stack operations: push , pop , peek , empty , search .

3. Set Interface

A Set contains no duplicate elements. Implementations are HashSet , LinkedHashSet , and TreeSet . Equality is based on equals() and, for hash‑based sets, matching hashCode() .

HashSet

Backed by a HashMap ; unordered, non‑synchronous, allows a single null element. Provides constant‑time basic operations.

LinkedHashSet

Maintains insertion order using a linked list on top of a hash table. Slightly slower inserts than HashSet but faster iteration.

TreeSet

Implements SortedSet using a red‑black tree. Elements are ordered either by natural ordering (via Comparable ) or a custom Comparator . Does not allow null keys.

4. Map Interface

Maps store key‑value pairs; keys must be unique. Implementations include HashMap , LinkedHashMap , TreeMap , and the legacy Hashtable .

HashMap

Hash‑table based, allows one null key and multiple null values, non‑synchronous. Fast look‑ups.

LinkedHashMap

Extends HashMap with a doubly‑linked list to preserve insertion order (or access order if configured).

TreeMap

Red‑black tree implementation; keys are sorted. Does not permit null keys. Non‑synchronous.

5. Iterator and ListIterator

Iterator provides forward‑only traversal with hasNext() , next() , and remove() . It is safe for removal during iteration.

public interface Iterator
{ boolean hasNext(); E next(); void remove(); }

ListIterator extends Iterator with bidirectional navigation, index queries, and element modification methods ( add , set ).

public interface ListIterator
extends Iterator
{
    boolean hasPrevious();
    E previous();
    int nextIndex();
    int previousIndex();
    void set(E e);
    void add(E e);
}

6. Differences Between Implementations

ArrayList vs LinkedList : ArrayList offers fast random access; LinkedList excels at frequent insertions/removals at the ends.

HashMap vs Hashtable : HashMap is non‑synchronous and permits null keys/values; Hashtable is synchronized and disallows null .

HashMap vs LinkedHashMap vs TreeMap : HashMap provides unordered iteration; LinkedHashMap preserves insertion order; TreeMap maintains sorted order.

HashSet vs LinkedHashSet vs TreeSet : HashSet is unordered; LinkedHashSet preserves insertion order; TreeSet keeps elements sorted.

7. Collection vs Collections

java.util.Collection is the root interface for all collection types (lists, sets, queues). It defines generic operations such as add , remove , size , etc.

java.util.Collections is a utility class offering static methods for sorting, searching, synchronizing, and other bulk operations on collections.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestCollections {
    public static void main(String[] args) {
        List
list = new ArrayList<>();
        double[] array = {112, 111, 23, 456, 231};
        for (double v : array) {
            list.add(v);
        }
        Collections.sort(list);
        for (Double d : list) {
            System.out.println(d);
        }
    }
}

8. Sample Code Demonstrations

Iterating over a HashMap :

HashMap
hashMap = new HashMap<>();
hashMap.put("4", "d");
hashMap.put("3", "c");
hashMap.put("2", "b");
hashMap.put("1", "a");
Iterator
it = hashMap.keySet().iterator();
while (it.hasNext()) {
    String key = it.next();
    System.out.println(key + "--" + hashMap.get(key));
}

Iterating over a LinkedHashMap (preserves insertion order):

LinkedHashMap
linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("4", "d");
linkedHashMap.put("3", "c");
linkedHashMap.put("2", "b");
linkedHashMap.put("1", "a");
Iterator
it = linkedHashMap.keySet().iterator();
while (it.hasNext()) {
    String key = it.next();
    System.out.println(key + "--" + linkedHashMap.get(key));
}

Iterating over a TreeMap (sorted by key):

TreeMap
treeMap = new TreeMap<>();
treeMap.put("4", "d");
treeMap.put("3", "c");
treeMap.put("2", "b");
treeMap.put("1", "a");
Iterator
it = treeMap.keySet().iterator();
while (it.hasNext()) {
    String key = it.next();
    System.out.println(key + "--" + treeMap.get(key));
}

9. Set Demo

public class SetDemo {
    public static void main(String[] args) {
        HashSet
hs = new HashSet<>();
        hs.add("B"); hs.add("A"); hs.add("D"); hs.add("E"); hs.add("C"); hs.add("F");
        System.out.println("HashSet order:\n" + hs);

        LinkedHashSet
lhs = new LinkedHashSet<>();
        lhs.add("B"); lhs.add("A"); lhs.add("D"); lhs.add("E"); lhs.add("C"); lhs.add("F");
        System.out.println("LinkedHashSet order:\n" + lhs);

        TreeSet
ts = new TreeSet<>();
        ts.add("B"); ts.add("A"); ts.add("D"); ts.add("E"); ts.add("C"); ts.add("F");
        System.out.println("TreeSet order:\n" + ts);
    }
}

Output demonstrates the ordering characteristics of each Set implementation.

Source: cnblogs.com/xiaoxi/p/6089984.html

JavaIteratorsCollectionsData Structuresmaps
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.