☕Java Programming
Java Collections Framework
List, Set, Map, Queue — ArrayList, LinkedList, HashMap, TreeMap, and iterators.
BeginnerIntermediateAdvanced
What You Will Learn in Java Collections Framework
The Java Collections Framework provides reusable data structures (List, Set, Map, Queue) and algorithms that operate on them through a unified interface.
- List (ordered, duplicates allowed): ArrayList (fast random access), LinkedList (fast insert/delete).
- Set (no duplicates): HashSet (O(1) ops, unordered), TreeSet (sorted), LinkedHashSet (insertion order).
- Map (key-value): HashMap (O(1), unordered), TreeMap (sorted by key), LinkedHashMap (insertion order).
- Queue/Deque: PriorityQueue (heap-based), ArrayDeque (stack/queue ops).
- Iterator pattern: use enhanced for-loop or Iterator to traverse collections.
- Collections utility class provides sort(), binarySearch(), reverse(), shuffle() methods.
Syntax
List<Type> list = new ArrayList<>(); Map<K, V> map = new HashMap<>(); Set<Type> set = new HashSet<>(); // Common operations list.add(elem); list.get(idx); list.remove(idx); map.put(key, val); map.get(key); map.containsKey(key); set.add(elem); set.contains(elem);
Complete Code Example
import java.util.*;
public class CollectionsDemo {
public static void main(String[] args) {
// List
List<String> fruits = new ArrayList<>(Arrays.asList("Mango","Apple","Banana"));
Collections.sort(fruits);
System.out.println(fruits); // [Apple, Banana, Mango]
// Map
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 95); scores.put("Bob", 87);
scores.forEach((k, v) -> System.out.println(k + " → " + v));
// Set
Set<Integer> nums = new TreeSet<>(Arrays.asList(5,3,1,4,2));
System.out.println(nums); // [1, 2, 3, 4, 5]
}
}Example
A HashMap stores student names as keys and their marks as values for O(1) lookup.
Expected Exam Questions — Java Collections Framework
Q1.What is the difference between ArrayList and LinkedList?
Answer: ArrayList is backed by a dynamic array — O(1) random access, O(n) insert/delete at middle. LinkedList is doubly-linked — O(n) random access, O(1) insert/delete at ends. Use ArrayList for read-heavy, LinkedList for write-heavy workloads.
Q2.How does HashMap work internally?
Answer: HashMap uses an array of buckets. On `put(key, value)`, it computes `hashCode()` of key, finds the bucket (index = hash % capacity), then uses `equals()` to handle collisions (chaining with linked list, or tree for >= 8 entries in Java 8+).
Q3.What is the difference between `Comparable` and `Comparator`?
Answer: `Comparable` (java.lang) defines natural ordering inside the object class via `compareTo()` — used by TreeSet, TreeMap. `Comparator` (java.util) is an external strategy defining custom ordering via `compare()` — passed to `Collections.sort()` or `TreeSet` constructor.
🔘 MCQ Practice — Java Collections Framework
MCQ 1.Which Collection maintains insertion order and allows duplicates?
A. HashSet
B. TreeSet
C. ArrayList ✓
D. HashMap
✓ Correct Answer: ArrayList
MCQ 2.What is the time complexity of `get()` in HashMap (no collision)?
A. O(n)
B. O(log n)
C. O(1) ✓
D. O(n log n)
✓ Correct Answer: O(1)
Download Java Collections Framework PDF Notes
Get the complete Java Collections Framework notes as a PDF — free for enrolled students, or browse our public study materials library.