Java Collection Framework (JCF)
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
β
TreeSetMap (separate interface)
βββ HashMap
βββ TreeMap
βββ LinkedHashMapβ Summary Table
π 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);
}
}








