Java Collections Framework



What is JCF?

The Java Collections Framework (JCF) is a unified architecture provided in the java.util package for storing, manipulating, and processing groups of objects. It provides a standardized set of interfaces, implementations, algorithms, and utility classes that help developers efficiently manage data in Java applications.


JCF eliminates the need to create custom data structures from scratch by offering ready-to-use implementations such as ArrayList, LinkedList, HashSet, TreeSet, HashMap, and TreeMap.


Before JCF was introduced, Java provided individual collection classes such as Vector, Stack, and Hashtable. However, these classes lacked a common design and consistent APIs. To address this limitation, the Java Collections Framework was introduced in Java 1.2, providing a unified and extensible framework for handling collections.



Core Components of JCF


The Java Collections Framework consists of four major components:


1. Interfaces

Interfaces define the behavior and contract for different types of collections.


Examples:

  • List
  • Set
  • Queue
  • Deque
  • Map

2. Implementations

Implementation classes provide concrete data structures based on the collection interfaces.


Examples:

  • ArrayList
  • LinkedList
  • HashSet
  • TreeSet
  • HashMap
  • TreeMap

3. Algorithms

The Collections utility class provides common algorithms that operate on collections.


Examples:

  • Sorting
  • Searching
  • Shuffling
  • Reversing
  • Finding minimum and maximum elements

4. Iterators

Iterators provide a standard mechanism for traversing collection elements.


Examples:

  • Iterator
  • ListIterator
  • Spliterator.



Evolution of JCF 

Java Version                            

Major Collection Enhancements

Java 1.2

Introduced the Java Collections Framework (JCF) with core interfaces and implementations such as Collection, List, Set, Map, ArrayList, LinkedList, HashSet, TreeSet, HashMap, and TreeMap.

Java 1.5

Added Generics for type safety and the Enhanced For-Each Loop for easier collection traversal.

Java 1.6

Introduced concurrent sorted collections such as ConcurrentSkipListMap and ConcurrentSkipListSet.

Java 1.7

Added the Diamond Operator (<>) to reduce verbosity when creating collection objects.

Java 1.8

Major enhancement with Lambda Expressions, Streams API, forEach(), removeIf(), replaceAll(), computeIfAbsent(), and other functional-style collection operations.

Java 9

Introduced immutable collection factory methods: List.of(), Set.of(), and Map.of().

Java 10

Added immutable collection copy methods: List.copyOf(), Set.copyOf(), and Map.copyOf().

Java 11–17

Various performance optimizations, Stream API improvements, and internal collection enhancements.

Java 18

Introduced Stream.toList() as a convenient immutable terminal operation.

Java 19–20

Project Loom improvements indirectly benefited concurrent collections and multi-threaded applications through Virtual Threads (Preview).

Java 21 (LTS)

Introduced Sequenced Collections API: SequencedCollection, SequencedSet, and SequencedMap, providing uniform methods such as getFirst(), getLast(), addFirst(), and addLast().



Features of JCF

  • Unified Architecture

Provides a common set of interfaces and implementations for managing collections.


Examples:

    • List
    • Set
    • Queue
    • Deque
    • Map
  • Dynamic Resizing

Unlike arrays, collections can automatically grow and shrink as elements are added or removed.


  • Rich Set of Data Structures

Offers ready-made implementations for different requirements.


Examples:

    • ArrayList
    • LinkedList
    • HashSet
    • TreeSet
    • HashMap
    • TreeMap
  • Type Safety with Generics

Generics help prevent runtime type errors by enforcing compile-time type checking.


List<String> names = new ArrayList<>();


  • Built-in Algorithms

The Collections utility class provides powerful algorithms such as:

    • sort()
    • binarySearch()
    • reverse()
    • shuffle()
    • min()
    • max()
  • Performance Optimization

Different collection implementations are optimized for different use cases.


Collection

Best For

ArrayList

Fast random access

LinkedList

Frequent insertions and deletions

HashSet

Fast uniqueness checks

HashMap

Fast key-value lookups

TreeSet

Sorted unique data

TreeMap

Sorted key-value storage


  • Thread-Safe Variants

JCF provides synchronized and concurrent collection implementations for multi-threaded applications.


Examples:

    • Vector
    • Hashtable
    • ConcurrentHashMap
    • CopyOnWriteArrayList
    • CopyOnWriteArraySet
  • Utility Classes

Java provides several utility classes that simplify collection operations.


Examples:

    • Collections
    • Arrays
    • Objects
  • Functional Programming Support

JCF integrates seamlessly with Lambda Expressions and the Streams API.


List<String> names = List.of("John", "David", "Alex");


names.stream()

     .filter(name -> name.startsWith("A"))

     .forEach(System.out::println);

  • Concurrency Support

The java.util.concurrent package provides highly scalable thread-safe collections.


Examples:

    • ConcurrentHashMap
    • ConcurrentLinkedQueue
    • ArrayBlockingQueue
    • LinkedBlockingQueue
  • Simple Example

import java.util.*;


public class CollectionsExample {


    public static void main(String[] args) {


        List<String> employees = new ArrayList<>();

        employees.add("John");

        employees.add("David");


        Map<Integer, String> employeeMap = new HashMap<>();

        employeeMap.put(101, "John");

        employeeMap.put(102, "David");


        System.out.println(employees);

        System.out.println(employeeMap);

    }

}


Output:

[John, David]

{101=John, 102=David}


This example demonstrates how the Java Collections Framework provides ready-made data structures for storing and managing data efficiently.



JCF Hierarchy

java.lang.Object
|
|── java.lang.Iterable<E>                   - Root interface for traversable collections
|   |
|   └── java.util.Collection<E>             - Root interface representing a group of objects
|       |
|       |── java.util.List<E>               - Ordered collection that allows duplicates
|       |   |
|       |   |── ArrayList                   - Dynamic array implementation
|       |   |── LinkedList                  - Doubly linked list implementation
|       |   |── Vector                      - Legacy synchronized List
|       |   |   └── Stack                   - Legacy LIFO stack implementation
|       |   |
|       |   └── CopyOnWriteArrayList        - Thread-safe List optimized for read-heavy workloads
|       |
|       |── java.util.Set<E>                - Collection that stores unique elements
|       |   |
|       |   |── HashSet                     - Hash table based Set
|       |   |   └── LinkedHashSet           - Maintains insertion order
|       |   |
|       |   |── SortedSet<E>                - Maintains elements in sorted order
|       |   |   |
|       |   |   └── NavigableSet<E>         - Supports navigation operations on sorted elements
|       |   |       |
|       |   |       └── TreeSet             - Red-Black tree based sorted Set
|       |   |
|       |   |── EnumSet                     - High-performance Set for enum values
|       |   |
|       |   └── CopyOnWriteArraySet         - Thread-safe Set optimized for read-heavy workloads
|       |
|       └── java.util.Queue<E>              - Collection designed for element processing
|           |
|           |── PriorityQueue               - Processes elements according to priority
|           |
|           |── Deque<E>                    - Double-ended queue supporting FIFO and LIFO operations
|           |   |
|           |   |── ArrayDeque              - Resizable array implementation of Deque
|           |   └── LinkedList              - List and Deque implementation
|           |
|           |── ConcurrentLinkedQueue       - Non-blocking thread-safe FIFO queue
|           |
|           └── BlockingQueue<E>            - Queue supporting producer-consumer patterns
|               |
|               |── ArrayBlockingQueue      - Fixed-capacity blocking queue
|               |── LinkedBlockingQueue     - Dynamically sized blocking queue
|               |── PriorityBlockingQueue   - Priority-based blocking queue
|               |── DelayQueue              - Queue for delayed element processing
|               └── SynchronousQueue        - Direct handoff queue between threads
|
|── java.util.Map<K,V>                      - Stores key-value pairs (separate hierarchy)
|   |
|   |── HashMap                             - General-purpose hash table based Map
|   |   |
|   |   └── LinkedHashMap                   - Maintains insertion or access order
|   |
|   |── SortedMap<K,V>                      - Maintains keys in sorted order
|   |   |
|   |   └── NavigableMap<K,V>               - Supports navigation operations on sorted keys
|   |       |
|   |       └── TreeMap                     - Red-Black tree based sorted Map
|   |
|   |── Hashtable                           - Legacy synchronized Map
|   |   |
|   |   └── Properties                      - Configuration key-value storage
|   |
|   |── WeakHashMap                         - Entries removed when keys become unreachable
|   |
|   |── IdentityHashMap                     - Uses reference equality (==) instead of equals()
|   |
|   |── EnumMap                             - High-performance Map for enum keys
|   |
|   └── ConcurrentMap<K,V>                  - Thread-safe Map interface
|       |
|       |── ConcurrentHashMap               - High-performance concurrent Map
|       |
|       └── ConcurrentNavigableMap<K,V>     - Thread-safe sorted Map interface
|           |
|           └── ConcurrentSkipListMap       - Concurrent sorted Map based on Skip List
|
|── Utility Classes                         - Helper classes for collection operations
|   |
|   |── Collections            - Algorithms (sorting, searching, shuffling), synchronization wrappers
|   |── Arrays                 - Array manipulation, sorting, searching, and conversion utilities
|   └── Objects                - Null-safe utility methods and object helper functions
|
|── Iterator Interfaces        - Collection traversal mechanisms
|   |
|   |── Iterator               - Forward-only collection traversal
|   |── ListIterator           - Bi-directional List traversal with modification support
|   |── Spliterator            - Parallel and bulk traversal support
|   └── Enumeration            - Legacy traversal interface
|
└── Comparison Interfaces      - Define sorting and ordering behavior
    |
    |── Comparable<T>          - Defines natural ordering of objects
    └── Comparator<T>          - Defines custom ordering of objects

Important

Collection Hierarchy
 ├── List
 ├── Set
 └── Queue

Map Hierarchy
 └── Map

Note: Map is part of the Java Collections Framework (JCF), but it does not extend the Collection interface.

Therefore, Map forms a separate hierarchy within JCF.



JCF Decision Tree

Start │ ├── Do you need to store Key-Value pairs? │ │ │ ├── Yes → Use Map │ │ │ │ │ ├── Need sorted keys? │ │ │ └── Yes
│ │ │ └── Concurrent access required?
│ │ │ ├── Yes → ConcurrentSkipListMap │ │ │ └── No → TreeMap │ │ │ │ │ ├── Need insertion order preserved? │ │ │ └── LinkedHashMap │ │ │ │ │ ├── Need thread safety? │ │ │ ├── High concurrency → ConcurrentHashMap │ │ │ ├── Legacy synchronized → Hashtable │ │ │ └── Collections.synchronizedMap() │ │ │ │ │ ├── Keys are Enum constants? │ │ │ └── EnumMap │ │ │ │ │ ├── Need weak references for keys? │ │ │ └── WeakHashMap │ │ │ │ │ ├── Need identity comparison (==) instead of equals()? │ │ │ └── IdentityHashMap │ │ │ │ │ └── General-purpose map (default choice if no special requirement exists) → HashMap │ │ │ └── No → Use Collection │ ├── Are duplicate elements allowed? │ │ │ ├── Yes │ │ │ │ │ └── Need index-based access?
│ │ │ │ │ ├── Yes → Use List │ │ │ │ │ │ │ ├── Fast random access / frequent reads? │ │ │ │ └── ArrayList (default choice) │ │ │ │ │ │ │ ├── Frequent insertions/deletions? │ │ │ │ └── LinkedList │ │ │ │ │ │ │ ├── Thread-safe, read-heavy workload? │ │ │ │ └── CopyOnWriteArrayList │ │ │ │ │ │ │ └── Legacy synchronized list? │ │ │ └── Vector │ │ │ │ │ └── No → Use Queue / Deque │ │ │ │ │ ├── Need FIFO processing? │ │ │ ├── High performance → ArrayDeque │ │ │ └── Queue + List operations → LinkedList │ │ │ │ │ ├── Need priority-based processing? │ │ │ └── PriorityQueue │ │ │ │ │ ├── Need concurrent queue? │ │ │ └── ConcurrentLinkedQueue │ │ │ │ │ ├── Need blocking producer-consumer queue? │ │ │ │ │ │ │ ├── Fixed capacity → ArrayBlockingQueue │ │ │ ├── Dynamic capacity → LinkedBlockingQueue │ │ │ ├── Priority ordering → PriorityBlockingQueue │ │ │ ├── Delayed processing → DelayQueue │ │ │ └── Direct handoff → SynchronousQueue │ │ │ │ │ └── Need both stack and queue operations? │ │ └── ArrayDeque │ │ │ └── No → Use Set │ │ │ ├── Need sorted elements? │ │ │ │ │ └── Concurrent access required?
│ │ ├── Yes → ConcurrentSkipListSet │ │ └── No → TreeSet │ │ │ ├── Need insertion order preserved? │ │ └── LinkedHashSet │ │ │ ├── Elements are Enum values? │ │ └── EnumSet │ │ │ ├── Need thread-safe set? │ │ └── CopyOnWriteArraySet │ │ │ └── General-purpose set (default choice if no special requirement exists) → HashSet
│ ├── Need Stack (LIFO) behavior? │ │ │ ├── Modern implementation → ArrayDeque │ └── Legacy implementation → Stack │ ├── Need Double-Ended Queue (Deque)? │ │ │ ├── Best performance → ArrayDeque │ └── List + Queue features → LinkedList │ └── Default Choices │ ├── Most common List → ArrayList ├── Most common Set → HashSet ├── Most common Map → HashMap ├── Ordered Map → LinkedHashMap ├── Sorted Map → TreeMap ├── Ordered Set → LinkedHashSet ├── Sorted Set → TreeSet ├── Concurrent Map → ConcurrentHashMap ├── Best Stack → ArrayDeque ├── Best Queue → ArrayDeque └── Priority Processing → PriorityQueue


JCF Cheat Sheet :


Requirement

Recommended Collection

General-purpose List

ArrayList

Fast random access

ArrayList

Frequent insertions/deletions

LinkedList

Thread-safe read-heavy List

CopyOnWriteArrayList

Legacy synchronized List

Vector

General-purpose Set

HashSet

Maintain insertion order (Set)

LinkedHashSet

Maintain sorted order (Set)

TreeSet

Concurrent sorted Set

ConcurrentSkipListSet

Enum values only

EnumSet

Thread-safe Set

CopyOnWriteArraySet

General-purpose Map

HashMap

Maintain insertion order (Map)

LinkedHashMap

Maintain sorted keys

TreeMap

Concurrent sorted Map

ConcurrentSkipListMap

High concurrency Map

ConcurrentHashMap

Legacy synchronized Map

Hashtable

Enum keys

EnumMap

Weak-reference keys

WeakHashMap

Identity comparison (==)

IdentityHashMap

FIFO Queue

ArrayDeque

Queue + List operations

LinkedList

Priority-based processing

PriorityQueue

Concurrent Queue

ConcurrentLinkedQueue

Fixed-capacity Blocking Queue

ArrayBlockingQueue

Dynamic-capacity Blocking Queue

LinkedBlockingQueue

Priority-based Blocking Queue

PriorityBlockingQueue

Delayed task execution

DelayQueue

Direct thread handoff

SynchronousQueue

Modern Stack (LIFO)

ArrayDeque

Legacy Stack

Stack

Double-Ended Queue (Deque)

ArrayDeque

High-performance Queue/Stack

ArrayDeque


JCF Default Choices for Most Applications :


Collection Type

Recommended Default

List

ArrayList

Set

HashSet

Map

HashMap

Queue

ArrayDeque

Stack

ArrayDeque

Concurrent Map

ConcurrentHashMap

Ordered Map

LinkedHashMap

Ordered Set

LinkedHashSet

Sorted Map

TreeMap

Sorted Set

TreeSet


JCF Golden Rule :


Duplicates      → List

Uniqueness      → Set

Key-Value       → Map

FIFO            → Queue

LIFO            → ArrayDeque

Sorted          → TreeMap / TreeSet

Insertion Order → LinkedHashMap / LinkedHashSet

Concurrency     → Concurrent Collections

Producer-Consumer → BlockingQueue.



Time Complexity Comparision

Understanding the time complexity of collection operations helps in selecting the right collection for better performance.


Collection

Access

Search

Insert

Delete

ArrayList

O(1)

O(n)

O(1)*

O(n)

LinkedList

O(n)

O(n)

O(1)

O(1)

HashSet

-

O(1)

O(1)

O(1)

LinkedHashSet

-

O(1)

O(1)

O(1)

TreeSet

-

O(log n)

O(log n)

O(log n)

HashMap

-

O(1)

O(1)

O(1)

LinkedHashMap

-

O(1)

O(1)

O(1)

TreeMap

-

O(log n)

O(log n)

O(log n)

PriorityQueue

O(1)

O(n)

O(log n)

O(log n)

ConcurrentHashMap

-

O(1)

O(1)

O(1)


Note: O(1)* for ArrayList insertion means amortized constant time when adding elements at the end.



Collection vs Collections

Collection

Collections

Interface

Utility Class

Part of collection hierarchy

Helper class for collection operations

Parent of List, Set, and Queue

Provides static utility methods

Used to store data

Used to manipulate data

Cannot be instantiated directly

Cannot be instantiated


Example


Collection


Collection<String> names = new ArrayList<>();


names.add("John");

names.add("David");


System.out.println(names);


// Output:

// [John, David]


Collections


List<String> names =

        Arrays.asList("David", "John", "Alex");


Collections.sort(names);


System.out.println(names);


// Output:

// [Alex, David, John]



JCF Best Practices

1. Program to Interfaces, Not Implementations

Prefer:


List<String> employees = new ArrayList<>();

Set<String> skills = new HashSet<>();

Map<Integer, String> users = new HashMap<>();


Instead of:


ArrayList<String> employees = new ArrayList<>();

HashSet<String> skills = new HashSet<>();

HashMap<Integer, String> users = new HashMap<>();


This makes your code more flexible and easier to maintain.


2. Use ArrayList as the Default List


List<String> names = new ArrayList<>();


Use LinkedList only when frequent insertions and deletions are required.


3. Use HashSet as the Default Set


Set<String> emails = new HashSet<>();


Use TreeSet only when sorted elements are required.


4. Use HashMap as the Default Map


Map<Integer, String> employees = new HashMap<>();


Use TreeMap only when sorted keys are required.


5. Prefer ArrayDeque over Stack


Deque<String> stack = new ArrayDeque<>();


stack.push("A");

stack.push("B");


System.out.println(stack.pop());


// Output:

// B


ArrayDeque is faster and recommended over the legacy Stack class.


6. Prefer ConcurrentHashMap over Hashtable


Map<String, Integer> counter =

        new ConcurrentHashMap<>();


Provides better scalability and concurrency support than Hashtable.


7. Use Generics for Type Safety

Preferred:


List<String> names = new ArrayList<>();


Avoid:


List names = new ArrayList();


Generics provide compile-time type safety and eliminate unnecessary type casting.


8. Use Immutable Collections When Data Should Not Change


List<String> countries =

        List.of("India", "USA", "UK");


System.out.println(countries);


// Output:

// [India, USA, UK]


Immutable collections prevent accidental modifications.


9. Choose the Right Collection Based on Requirements


Need duplicates?       → List

Need uniqueness?       → Set

Need key-value pairs?  → Map

Need sorting?          → TreeSet / TreeMap

Need insertion order?  → LinkedHashSet / LinkedHashMap

Need concurrency?      → Concurrent Collections

Need FIFO processing?  → Queue

Need LIFO processing?  → ArrayDeque


10. Avoid Legacy Collections in New Applications

Avoid using:


Vector

Stack

Hashtable

Enumeration


Prefer modern alternatives:

Legacy

Recommended Alternative

Vector

ArrayList / CopyOnWriteArrayList

Stack

ArrayDeque

Hashtable

ConcurrentHashMap

Enumeration

Iterator



Summary

The Java Collections Framework (JCF) provides a unified architecture for storing, manipulating, and processing groups of objects in Java. It includes powerful interfaces such as List, Set, Queue, Deque, and Map, along with numerous implementations like ArrayList, LinkedList, HashSet, TreeSet, HashMap, and ConcurrentHashMap.


JCF offers ready-to-use data structures, built-in algorithms, iterators, concurrency support, and functional programming integration through Streams and Lambda Expressions. By understanding the characteristics, performance, and use cases of each collection type, developers can choose the most appropriate collection and build efficient, scalable, and maintainable Java applications.


Whether you're preparing for interviews, learning Core Java, or building enterprise applications, mastering JCF is an essential skill for every Java developer.



 Examples 

List Interface


Ordered collection that allows duplicates.


🔹 ArrayList – Shopping Cart

List<String> cart = new ArrayList<>();


cart.add("Laptop");

cart.add("Mouse");

cart.add("Keyboard");


System.out.println(cart);


// Output:

// [Laptop, Mouse, Keyboard]


Use Case: E-commerce shopping cart.


🔹 LinkedList – Train Coach Management

LinkedList<String> train = new LinkedList<>();


train.add("Coach-A");

train.add("Coach-B");


train.addFirst("Engine");

train.addLast("Guard");


System.out.println(train);


// Output:

// [Engine, Coach-A, Coach-B, Guard]


Use Case: Frequent insertions and deletions.


🔹 Vector – Legacy Banking Transactions

Vector<String> transactions = new Vector<>();


transactions.add("Deposit");

transactions.add("Withdrawal");


System.out.println(transactions);


// Output:

// [Deposit, Withdrawal]


Use Case: Legacy synchronized applications.


🔹 Stack – Undo Operation

Stack<String> actions = new Stack<>();


actions.push("Type A");

actions.push("Type B");


System.out.println(actions.pop());


// Output:

// Type B


Use Case: Undo functionality in editors.


Set Interface


Stores unique elements only.


🔹 HashSet – Registered Email Addresses

Set<String> emails = new HashSet<>();


emails.add("john@gmail.com");

emails.add("alice@gmail.com");

emails.add("john@gmail.com");


System.out.println(emails);


// Possible Output:

// [john@gmail.com, alice@gmail.com]


Use Case: Unique user registration emails.


🔹 LinkedHashSet – Website Visitor Tracking

Set<String> visitors = new LinkedHashSet<>();


visitors.add("John");

visitors.add("David");

visitors.add("Alex");


System.out.println(visitors);


// Output:

// [John, David, Alex]


Use Case: Preserve insertion order.


🔹 TreeSet – Leaderboard Scores

Set<Integer> scores = new TreeSet<>();


scores.add(95);

scores.add(80);

scores.add(90);


System.out.println(scores);


// Output:

// [80, 90, 95]


Use Case: Automatically sorted rankings.


🔹 EnumSet – User Roles

enum Role {

    ADMIN, USER, GUEST

}


EnumSet<Role> roles =

        EnumSet.of(Role.ADMIN, Role.USER);


System.out.println(roles);


// Output:

// [ADMIN, USER]


Use Case: Store enum values efficiently.


Queue Interface


FIFO (First In First Out) processing.


🔹 LinkedList as Queue – Bank Token System

Queue<String> customers = new LinkedList<>();


customers.offer("Customer-101");

customers.offer("Customer-102");


System.out.println(customers.poll());


// Output:

// Customer-101


Use Case: Customer service queues.


🔹 PriorityQueue – Task Scheduler

PriorityQueue<Integer> tasks =

        new PriorityQueue<>();


tasks.offer(3);

tasks.offer(1);

tasks.offer(2);


System.out.println(tasks.poll());


// Output:

// 1


Use Case: Process high-priority tasks first.


🔹 ConcurrentLinkedQueue – Async Task Queue

Queue<String> queue =

        new ConcurrentLinkedQueue<>();


queue.offer("Task-1");

queue.offer("Task-2");


System.out.println(queue.poll());


// Output:

// Task-1


Use Case: Multi-threaded task processing.


Deque Interface


Supports insertion and deletion from both ends.


🔹 ArrayDeque – Undo/Redo System

Deque<String> actions =

        new ArrayDeque<>();


actions.push("Edit-1");

actions.push("Edit-2");


System.out.println(actions.pop());


// Output:

// Edit-2


Use Case: Modern replacement for Stack.


🔹 LinkedList as Deque – Browser Navigation

Deque<String> pages =

        new LinkedList<>();


pages.add("Home");

pages.add("Products");


System.out.println(pages.removeLast());


// Output:

// Products


Use Case: Back and forward navigation.


Map Interface


Stores data as key-value pairs.


🔹 HashMap – Employee Directory

Map<Integer, String> employees =

        new HashMap<>();


employees.put(101, "John");

employees.put(102, "David");


System.out.println(employees.get(101));


// Output:

// John


Use Case: Fast employee lookup by ID.


🔹 LinkedHashMap – Recently Viewed Products

Map<Integer, String> products =

        new LinkedHashMap<>();


products.put(1, "Laptop");

products.put(2, "Mouse");

products.put(3, "Keyboard");


System.out.println(products);


// Output:

// {1=Laptop, 2=Mouse, 3=Keyboard}


Use Case: Preserve viewing order.


🔹 TreeMap – Student Ranking System

Map<Integer, String> ranks =

        new TreeMap<>();


ranks.put(3, "Alex");

ranks.put(1, "John");

ranks.put(2, "David");


System.out.println(ranks);


// Output:

// {1=John, 2=David, 3=Alex}


Use Case: Automatically sorted records.


🔹 Hashtable – Legacy Configuration Store

Hashtable<String, String> config =

        new Hashtable<>();


config.put("theme", "dark");


System.out.println(config.get("theme"));


// Output:

// dark


Use Case: Legacy synchronized applications.


🔹 WeakHashMap – Temporary Cache

WeakHashMap<Object, String> cache =

        new WeakHashMap<>();


Object key = new Object();


cache.put(key, "SessionData");


System.out.println(cache.size());


// Output:

// 1


Use Case: Auto-remove entries when keys become unreachable.


🔹 IdentityHashMap – Object Identity Tracking

IdentityHashMap<String, String> map =

        new IdentityHashMap<>();


map.put(new String("A"), "First");

map.put(new String("A"), "Second");


System.out.println(map.size());


// Output:

// 2


Use Case: Compare keys using == instead of equals().


🔹 EnumMap – Weekly Schedule

enum Day {

    MON, TUE

}


EnumMap<Day, String> schedule =

        new EnumMap<>(Day.class);


schedule.put(Day.MON, "Gym");


System.out.println(schedule.get(Day.MON));


// Output:

// Gym


Use Case: Efficient enum-based lookups.


🔹 ConcurrentHashMap – Website Visitor Counter

Map<String, Integer> counter =

        new ConcurrentHashMap<>();


counter.put("Visitors", 500);


System.out.println(counter.get("Visitors"));


// Output:

// 500


Use Case: Multi-threaded web applications.


Utility Classes


🔹 Collections – Sorting Products

List<String> products =

        Arrays.asList("Mouse", "Laptop", "Keyboard");


Collections.sort(products);


System.out.println(products);


// Output:

// [Keyboard, Laptop, Mouse]


Use Case: Sorting collections.


🔹 Arrays – Sorting Prices

int[] prices = {500, 100, 300};


Arrays.sort(prices);


System.out.println(Arrays.toString(prices));


// Output:

// [100, 300, 500]


Use Case: Array operations.


🔹 Objects – Null-Safe Comparison

String a = null;

String b = "Java";


System.out.println(

        Objects.equals(a, b)

);


// Output:

// false


Use Case: Avoid NullPointerException.


Properties


🔹 Properties – Application Configuration

Properties config = new Properties();


config.setProperty("environment", "production");


System.out.println(

        config.getProperty("environment")

);


// Output:

// production


Use Case: Store application settings.


Comparison Interfaces


🔹 Comparable – Natural Sorting

class Student

        implements Comparable<Student> {


    String name;

    int age;


    Student(String name, int age) {

        this.name = name;

        this.age = age;

    }


    public int compareTo(Student s) {

        return this.age - s.age;

    }


    public String toString() {

        return name + " (" + age + ")";

    }

}


List<Student> students =

        new ArrayList<>();


students.add(new Student("John", 25));

students.add(new Student("Alice", 20));


Collections.sort(students);


System.out.println(students);


// Output:

// [Alice (20), John (25)]


Use Case: Natural ordering.


🔹 Comparator – Custom Sorting

Comparator<Student> byName =

        (s1, s2) ->

                s1.name.compareTo(s2.name);


Collections.sort(students, byName);


System.out.println(students);


// Output:

// [Alice (20), John (25)]


Use Case: Multiple sorting strategies.


Iterator Interfaces


🔹 Iterator – Safe Removal During Traversal

List<String> items =

        new ArrayList<>(

                Arrays.asList(

                        "Keep",

                        "Remove",

                        "Keep"

                )

        );


Iterator<String> iterator =

        items.iterator();


while (iterator.hasNext()) {


    if (iterator.next().equals("Remove")) {

        iterator.remove();

    }

}


System.out.println(items);


// Output:

// [Keep, Keep]


Use Case: Safe element removal during iteration.


🔹 ListIterator – Bi-Directional Traversal

List<String> colors =

        new ArrayList<>(

                Arrays.asList(

                        "Red",

                        "Green",

                        "Blue"

                )

        );


ListIterator<String> itr =

        colors.listIterator();


while (itr.hasNext()) {


    if (itr.next().equals("Green")) {

        itr.set("Lime");

    }

}


System.out.println(colors);


// Output:

// [Red, Lime, Blue]


Use Case: Modify List while traversing.


🔹 Enumeration – Legacy Traversal

Vector<String> vector =

        new Vector<>();


vector.add("One");

vector.add("Two");


Enumeration<String> e =

        vector.elements();


while (e.hasMoreElements()) {

    System.out.println(e.nextElement());

}


// Output:

// One

// Two


Use Case: Legacy collections.


🔹 Spliterator – Bulk Traversal

List<String> fruits =

        Arrays.asList(

                "Apple",

                "Banana",

                "Mango"

        );


Spliterator<String> sp =

        fruits.spliterator();


sp.forEachRemaining(System.out::println);


// Output:

// Apple

// Banana

// Mango


Use Case: Stream and parallel processing.


Concurrent Collections


🔹 CopyOnWriteArrayList – Event Listeners

CopyOnWriteArrayList<String> listeners =

        new CopyOnWriteArrayList<>();


listeners.add("EmailListener");

listeners.add("SmsListener");


System.out.println(listeners);


// Output:

// [EmailListener, SmsListener]


Use Case: Read-heavy concurrent applications.


🔹 CopyOnWriteArraySet – Notification Subscribers

CopyOnWriteArraySet<String> subscribers =

        new CopyOnWriteArraySet<>();


subscribers.add("john@gmail.com");

subscribers.add("alice@gmail.com");


System.out.println(subscribers);


// Output:

// [john@gmail.com, alice@gmail.com]


Use Case: Unique thread-safe subscribers.


🔹 ConcurrentLinkedQueue – Background Jobs

Queue<String> jobs =

        new ConcurrentLinkedQueue<>();


jobs.offer("EmailJob");

jobs.offer("ReportJob");


System.out.println(jobs.poll());


// Output:

// EmailJob


Use Case: Concurrent job processing.