Master Java Collections Framework –
Complete Notes
1. Overview of Java Collections Framework
The Java Collections Framework (JCF) is a unified architecture for representing and
manipulating collections, enabling efficient storage, retrieval, and manipulation of data. It
includes interfaces like List, Set, Queue, and Map and their concrete implementations.
Major benefits include reusability, interoperability, and algorithmic consistency.
Core interfaces:
- Collection (root interface)
- List (ArrayList, LinkedList, Vector)
- Set (HashSet, LinkedHashSet, TreeSet)
- Queue (PriorityQueue, Deque)
- Map (HashMap, LinkedHashMap, TreeMap, Hashtable, ConcurrentHashMap)
2. ArrayList
- Underlying data structure: Dynamic array (Object[] elementData)
- Default capacity: 10
- Growth mechanism: newCapacity = oldCapacity + (oldCapacity >> 1)
- Operations:
- add(E e): O(1) amortized, resizes if needed
- get(int index): O(1)
- remove(int index): O(n) (shift elements)
- Implements RandomAccess and Serializable
- Not synchronized
- Fail-fast iterator (throws ConcurrentModificationException)
3. LinkedList
- Underlying data structure: Doubly linked list
- Stores references to both previous and next nodes
- Operations:
- add/removeFirst(): O(1)
- get(index): O(n)
- add(index, element): O(n)
- Can be used as List, Deque, or Queue
- Not synchronized
4. HashSet
- Implements Set interface, backed by a HashMap internally
- No duplicate elements
- No guaranteed order
- Allows one null element
- Underlying structure: HashMap<E, Object> with a dummy value
- add(): O(1), remove(): O(1), contains(): O(1) on average
5. LinkedHashSet
- Extends HashSet and maintains insertion order
- Internally uses LinkedHashMap
- Slightly lower performance than HashSet due to ordering
6. TreeSet
- Implements NavigableSet backed by TreeMap
- Sorted in natural or comparator-provided order
- Based on Red-Black Tree
- No null values allowed
- Operations: O(log n)
7. HashMap
- Stores key-value pairs using a hash table
- Uses array of Node<K,V> (buckets)
- Index = (n - 1) & hash
- Handles collisions via chaining (LinkedList) or tree (Red-Black Tree from Java 8)
- Load factor = 0.75 (default); triggers rehashing when exceeded
- Allows one null key, multiple null values
- Not synchronized
- Fail-fast iterators
8. LinkedHashMap
- Maintains insertion or access order
- Backed by doubly linked list running through entries
- Slightly slower than HashMap but predictable iteration order
9. TreeMap
- Implements NavigableMap using Red-Black Tree
- Sorted map; keys must be Comparable or use a Comparator
- No null keys allowed
- Operations: O(log n)
10. Hashtable
- Legacy class, synchronized version of HashMap
- No null keys or values allowed
- Slower due to synchronization
11. ConcurrentHashMap
- Thread-safe implementation without locking the whole map
- Segmented locks (pre-Java 8), CAS and synchronized blocks (Java 8+)
- Allows concurrent read/write
- No null keys/values
- Uses bins (linked list/tree) like HashMap
12. Queue and Deque
Queue:
- FIFO structure
- Implementations: LinkedList, PriorityQueue
Deque:
- Double-ended queue (insert/remove from both ends)
- Implementations: LinkedList, ArrayDeque
13. PriorityQueue
- Implements Queue using binary heap (min-heap by default)
- Elements ordered using Comparable or Comparator
- Null elements not allowed
- Operations: O(log n)
14. Advanced Concepts
- Fail-fast Iterators: Throw ConcurrentModificationException if structure changes
unexpectedly during iteration
- Generics: Provides compile-time type safety (e.g., List<String>)
- Streams: Operate on collections with filter, map, reduce patterns
15. Summary Table
| Class | Structure | Order | Nulls | Thread-safe | Avg Time |
|------------------|------------------|-------------|-------|--------------|----------|
| ArrayList | Resizable array | Insertion | Yes | No | O(1) |
| LinkedList | Doubly linked | Insertion | Yes | No | O(n) |
| HashSet | Hash table | No order | One | No | O(1) |
| LinkedHashSet | Hash + LinkedList| Insertion | One | No | O(1) |
| TreeSet | Red-Black Tree | Sorted | No | No | O(log n) |
| HashMap | Hash table | No order | One | No | O(1) |
| LinkedHashMap | Hash + LinkedList| Insertion | One | No | O(1) |
| TreeMap | Red-Black Tree | Sorted | No | No | O(log n) |
| Hashtable | Hash table | No order | No | Yes | O(1) |
| ConcurrentHashMap| Segmented Table | No order | No | Yes | O(1) |