DEV Community

DevCorner
DevCorner

Posted on

Java Collections Framework: Classes and Interfaces Explained with Code Snippets

The Java Collections Framework (JCF) is a fundamental part of Java that provides a set of interfaces and classes to store, manipulate, and process collections of objects efficiently. This blog covers all interfaces and classes in the Java Collection Framework, along with code snippets for better understanding.


1. Core Interfaces in Java Collections Framework

1.1 Collection Interface (Root Interface)

The Collection<E> interface is the root of the Java Collections Framework hierarchy. It defines the fundamental operations applicable to any collection.

Methods in Collection<E> Interface:

  • add(E e): Adds an element.
  • remove(Object o): Removes an element.
  • size(): Returns the number of elements.
  • contains(Object o): Checks if an element exists.
  • iterator(): Returns an iterator.
import java.util.*;

public class CollectionExample {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("Java");
        collection.add("Spring Boot");
        collection.add("Microservices");

        System.out.println(collection);  // [Java, Spring Boot, Microservices]
    }
}
Enter fullscreen mode Exit fullscreen mode

2. List Interface and Implementations

2.1 List Interface (List<E>)

The List interface extends Collection and represents an ordered collection (also known as a sequence).

Key Features:

✅ Maintains insertion order

✅ Allows duplicate elements

✅ Supports index-based access

Methods in List<E> Interface:

  • add(int index, E element): Inserts an element at the given index.
  • get(int index): Retrieves an element.
  • remove(int index): Removes an element at the given index.
  • set(int index, E element): Replaces an element at the given index.

2.2 ArrayList (Dynamic Array Implementation)

  • Best for fast read operations (O(1) for get(), O(n) for add() at specific index).
  • Uses dynamic resizing internally.
import java.util.*;

public class ArrayListExample {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Java");
        list.add("Spring");
        list.add("Hibernate");

        System.out.println(list.get(1));  // Output: Spring
    }
}
Enter fullscreen mode Exit fullscreen mode

2.3 LinkedList (Doubly Linked List Implementation)

  • Best for fast insertions/deletions (O(1) for add()/remove() at head/tail).
  • Uses node-based memory allocation.
import java.util.*;

public class LinkedListExample {
    public static void main(String[] args) {
        List<String> list = new LinkedList<>();
        list.add("Node1");
        list.add("Node2");
        list.addFirst("Head");
        list.addLast("Tail");

        System.out.println(list);  // [Head, Node1, Node2, Tail]
    }
}
Enter fullscreen mode Exit fullscreen mode

2.4 Vector (Thread-Safe ArrayList)

  • Similar to ArrayList but synchronized (thread-safe).
  • Slower than ArrayList due to synchronization overhead.
import java.util.*;

public class VectorExample {
    public static void main(String[] args) {
        List<String> vector = new Vector<>();
        vector.add("Java");
        vector.add("Spring Boot");
        System.out.println(vector);  // [Java, Spring Boot]
    }
}
Enter fullscreen mode Exit fullscreen mode

2.5 Stack (LIFO Data Structure)

  • Extends Vector and provides Last-In-First-Out (LIFO) behavior.
import java.util.*;

public class StackExample {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(10);
        stack.push(20);
        stack.push(30);

        System.out.println(stack.pop());  // Output: 30 (Last element)
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Set Interface and Implementations

3.1 Set Interface (Set<E>)

A Set is a collection that does not allow duplicate elements.

3.2 HashSet (Unordered, Uses Hashing)

  • No insertion order is maintained.
  • Fast lookup (O(1) for search, insert, and delete).
import java.util.*;

public class HashSetExample {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        set.add("Java");
        set.add("Spring");
        set.add("Spring Boot");
        set.add("Java");  // Duplicate, ignored

        System.out.println(set);  // Output: Unordered elements
    }
}
Enter fullscreen mode Exit fullscreen mode

3.3 LinkedHashSet (Ordered HashSet)

  • Maintains insertion order while avoiding duplicates.
import java.util.*;

public class LinkedHashSetExample {
    public static void main(String[] args) {
        Set<Integer> set = new LinkedHashSet<>();
        set.add(10);
        set.add(5);
        set.add(30);

        System.out.println(set);  // Output: [10, 5, 30]
    }
}
Enter fullscreen mode Exit fullscreen mode

3.4 TreeSet (Sorted Set, Uses Red-Black Tree)

  • Sorted in natural order (or via a custom comparator).
import java.util.*;

public class TreeSetExample {
    public static void main(String[] args) {
        Set<Integer> treeSet = new TreeSet<>();
        treeSet.add(30);
        treeSet.add(10);
        treeSet.add(20);

        System.out.println(treeSet);  // Output: [10, 20, 30] (Sorted order)
    }
}
Enter fullscreen mode Exit fullscreen mode

4. Queue Interface and Implementations

4.1 Queue Interface (Queue<E>)

  • Follows First-In-First-Out (FIFO) principle.
  • Methods: add(), remove(), peek(), poll().

4.2 PriorityQueue (Heap-Based Queue)

  • Elements are ordered based on priority.
import java.util.*;

public class PriorityQueueExample {
    public static void main(String[] args) {
        Queue<Integer> pq = new PriorityQueue<>();
        pq.add(30);
        pq.add(10);
        pq.add(20);

        System.out.println(pq.poll());  // Output: 10 (Lowest priority element)
    }
}
Enter fullscreen mode Exit fullscreen mode

5. Map Interface and Implementations

5.1 Map Interface (Map<K, V>)

A Map is a key-value pair collection where keys are unique.

5.2 HashMap (Unordered Map, Fast Lookups)

  • No insertion order is maintained.
import java.util.*;

public class HashMapExample {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("Java", 10);
        map.put("Spring Boot", 20);

        System.out.println(map.get("Java"));  // Output: 10
    }
}
Enter fullscreen mode Exit fullscreen mode

5.3 LinkedHashMap (Maintains Insertion Order)

import java.util.*;

public class LinkedHashMapExample {
    public static void main(String[] args) {
        Map<Integer, String> map = new LinkedHashMap<>();
        map.put(1, "Java");
        map.put(2, "Spring Boot");

        System.out.println(map);  // Output: {1=Java, 2=Spring Boot}
    }
}
Enter fullscreen mode Exit fullscreen mode

5.4 TreeMap (Sorted Map, Uses Red-Black Tree)

  • Keys are stored in sorted order.
import java.util.*;

public class TreeMapExample {
    public static void main(String[] args) {
        Map<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(3, "C++");
        treeMap.put(1, "Java");
        treeMap.put(2, "Python");

        System.out.println(treeMap);  // {1=Java, 2=Python, 3=C++}
    }
}
Enter fullscreen mode Exit fullscreen mode

This completes the Java Collections Framework overview. Which topic should I expand further?

Here’s a tabular representation of the Java Collections Framework, including all core interfaces and their implementing classes.


1. Collection Framework Overview

Root Interface Sub-Interfaces Implementing Classes Ordering Duplicates Thread-Safe
Collection<E> List<E> ArrayList Yes Yes No
LinkedList Yes Yes No
Vector Yes Yes Yes
Stack Yes (LIFO) Yes Yes
Set<E> HashSet No No No
LinkedHashSet Yes No No
TreeSet Yes (Sorted) No No
Queue<E> PriorityQueue Yes (Priority-based) Yes No
LinkedList (as Queue) Yes Yes No
Deque<E> ArrayDeque Yes (FIFO/LIFO) Yes No
LinkedList (as Deque) Yes (FIFO/LIFO) Yes No

2. Map Hierarchy (Key-Value Pair Collections)

Interface Implementing Classes Ordering Duplicates (Keys/Values) Thread-Safe
Map<K, V> HashMap No Keys: No, Values: Yes No
LinkedHashMap Yes (Insertion Order) Keys: No, Values: Yes No
TreeMap Yes (Sorted) Keys: No, Values: Yes No
Hashtable No Keys: No, Values: Yes Yes
ConcurrentMap<K, V> ConcurrentHashMap No Keys: No, Values: Yes Yes

3. Java Collections Framework - Complete Hierarchy

Interface Type Implementing Classes
Collection<E> Root Interface -
List<E> Ordered Collection ArrayList, LinkedList, Vector, Stack
Set<E> Unique Elements HashSet, LinkedHashSet, TreeSet
Queue<E> FIFO Collection PriorityQueue, LinkedList (Queue), ArrayDeque
Deque<E> Double-ended Queue ArrayDeque, LinkedList (Deque)
Map<K, V> Key-Value Collection HashMap, LinkedHashMap, TreeMap, Hashtable
SortedSet<E> Ordered Set TreeSet
SortedMap<K, V> Ordered Map TreeMap

This table summarizes all interfaces and classes in the Java Collections Framework. Let me know if you need additional explanations or comparisons between them!

Top comments (0)