Sitemap
Khairul Basar

Khairul Basar

Khairul Basar | Backend Engineer & Java Specialist. Writing on Spring Boot, system design, distributed systems, backend patterns, and software development.

Java Collection Framework (JCF)

3 min readJun 16, 2025

--

List, Set, Queue, Deque, and Map.

The Java Collection Framework (JCF) is a set of classes and interfaces that implement commonly reusable data structures like List, Set, Queue, Deque, and Map.

These are ready-made data structures with powerful algorithms, allowing developers to store, manipulate, and access groups of objects efficiently.

πŸ”· Why Use the Collection Framework?

  • Reduces development time.
  • Provides performance-optimized data structures.
  • Supports algorithms like sorting, searching, shuffling.
  • Interfaces + polymorphism for flexible design.

βœ… Major Interfaces in the Collection Framework

πŸ“Œ 1. Collection Interface (java.util.Collection)

Parent interface of List, Set, Queue.

Get Khairul Basar’s stories inΒ yourΒ inbox

Join Medium for free to get updates fromΒ thisΒ writer.

Interface DescriptionListOrdered collection, allows duplicatesSetUnordered, no duplicates allowedQueueFIFO order, useful for scheduling

πŸ”· 2. List Interface and Implementations

➀ List<E>

Maintains insertion order and allows duplicate elements.

βœ… Implementations:

  • ArrayList
  • LinkedList
  • Vector (legacy, thread-safe)

πŸ“Œ Example:

import java.util.*;

public class ListExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Java");
list.add("C++");
list.add("Python");
list.add("Java"); // Duplicates allowed
for (String lang : list)
System.out.println(lang);
}
}

πŸ”· 3. Set Interface and Implementations

➀ Set<E>

Stores unique elements. No duplicates.

βœ… Implementations:

  • HashSet β€” Fast, unordered
  • LinkedHashSet β€” Maintains insertion order
  • TreeSet β€” Sorted set using red-black tree

πŸ“Œ Example:

Set<Integer> set = new HashSet<>();
set.add(5);
set.add(10);
set.add(5); // Ignored (duplicate)

System.out.println(set); // No order guaranteed

πŸ”· 4. Queue Interface and Implementations

➀ Queue<E>

Used for FIFO (first-in-first-out) operations.

βœ… Implementations:

  • LinkedList
  • PriorityQueue β€” Min-heap (by default), sorted queue

πŸ“Œ Example:

Queue<String> queue = new LinkedList<>();
queue.offer("A");
queue.offer("B");
queue.poll(); // removes "A"

System.out.println(queue); // [B]

πŸ”· 5. Deque Interface

➀ Deque<E> (Double Ended Queue)

Can insert/remove elements from both ends.

βœ… Implementations:

  • ArrayDeque

πŸ“Œ Example:

Deque<String> deque = new ArrayDeque<>();
deque.addFirst("Front");
deque.addLast("Back");

System.out.println(deque); // [Front, Back]

πŸ”· 6. Map Interface (Not part of Collection, but in framework)

➀ Map<K, V>

Stores key-value pairs.

βœ… Implementations:

  • HashMap
  • TreeMap
  • LinkedHashMap

πŸ“Œ Example:

Map<String, Integer> map = new HashMap<>();
map.put("Java", 10);
map.put("Python", 8);

System.out.println(map.get("Java")); // 10

βœ… Hierarchy of Java Collections

Iterable
β”‚
Collection
β”Œβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
List Set Queue Deque
β”‚ β”‚
SortedSet PriorityQueue
β”‚
NavigableSet
β”‚
TreeSet
Map (separate interface)
β”œβ”€β”€ HashMap
β”œβ”€β”€ TreeMap
└── LinkedHashMap

βœ… Summary Table

Press enter or click to view image in full size

πŸ“Œ Common Real-World Use Cases

Scenario β€” β€” β€” > Recommended Class

Store student marks by ID β€” β€” β†’HashMap<Integer, Integer>

Store tasks to process in order β€” β€” β†’Queue or Deque

Get top 3 scores β€” β€” β†’PriorityQueue

Remove duplicate items from list β€” β€” β†’HashSet

Need sorted list of names β€” β€” β†’TreeSet

Navigation or autocomplete system β€” β€” β†’TreeMap

πŸ›  Example: Using All in One

import java.util.*;

public class CollectionDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>(List.of("Java", "Python", "C++", "Java"));
Set<String> uniqueSet = new HashSet<>(list);
Queue<String> queue = new LinkedList<>(list);
Map<String, Integer> map = new HashMap<>();
for (String lang : list)
map.put(lang, map.getOrDefault(lang, 0) + 1);
System.out.println("List: " + list);
System.out.println("Unique Set: " + uniqueSet);
System.out.println("Queue Front: " + queue.poll());
System.out.println("Map Frequency: " + map);
}
}

--

--

Written by Khairul Basar

Khairul Basar | Backend Engineer & Java Specialist. Writing on Spring Boot, system design, distributed systems, backend patterns, and software development.