Java Collections Framework – Complete Notes
Includes interfaces, classes, methods, iteration techniques, Comparable, Comparator, and interview tips.
1. What is Java Collections Framework (JCF)?
The Java Collections Framework is a set of interfaces and classes used to store, manipulate, and process groups
Benefits:
• Ready-made data structures
• Dynamic sizing
• Efficient searching and sorting
• Reduces coding effort
• Improves performance and reusability
2. Hierarchy
Iterable
■■■ Collection
■■■ List
■ ■■■ ArrayList
■ ■■■ LinkedList
■ ■■■ Vector -> Stack
■■■ Set
■ ■■■ HashSet
■ ■■■ LinkedHashSet
■ ■■■ TreeSet
■■■ Queue
■■■ PriorityQueue
■■■ ArrayDeque
■■■ LinkedList
Map (separate hierarchy)
■■■ HashMap
■■■ LinkedHashMap
■■■ TreeMap
■■■ Hashtable
■■■ WeakHashMap
3. Core Interfaces
List: Ordered, allows duplicates.
Set: No duplicates.
Queue: FIFO processing.
Deque: Double-ended queue.
Map: Key-value pairs; keys are unique.
4. Common Collection Methods
add(E e)
addAll(Collection c)
remove(Object o)
removeAll(Collection c)
clear()
contains(Object o)
containsAll(Collection c)
isEmpty()
size()
iterator()
toArray()
equals(Object o)
hashCode()
5. List Interface
Important methods:
get(int index)
set(int index, E element)
add(int index, E element)
remove(int index)
indexOf(Object o)
lastIndexOf(Object o)
listIterator()
subList(int from, int to)
6. ArrayList vs LinkedList
ArrayList:
• Fast random access: O(1)
• Slow insertion/deletion in middle
LinkedList:
• Fast insertion/deletion at ends or via iterator
• Slow random access: O(n)
7. Set Implementations
HashSet:
• Unordered
• Uses hashing
• Allows one null
LinkedHashSet:
• Maintains insertion order
TreeSet:
• Sorted order
• No null
• Based on Red-Black Tree
8. Queue and Deque
Queue methods:
offer(), add()
poll(), remove()
peek(), element()
Deque methods:
offerFirst(), offerLast()
pollFirst(), pollLast()
peekFirst(), peekLast()
push(), pop()
9. Map Interface
put(K,V)
get(Object key)
remove(Object key)
containsKey(Object key)
containsValue(Object value)
keySet()
values()
entrySet()
putIfAbsent()
replace()
getOrDefault()
10. HashMap vs LinkedHashMap vs TreeMap
HashMap:
• Unordered
• O(1) average
LinkedHashMap:
• Insertion order maintained
TreeMap:
• Sorted by keys
• O(log n)
• No null keys
11. Iteration Techniques
for-each loop
Iterator
ListIterator
forEach(lambda)
Example:
for(String s : list) { }
Iterator<String> it = [Link]();
while([Link]()){
[Link]([Link]());
}
12. Comparable
Used for natural/default sorting.
Defined in [Link] package.
Method:
int compareTo(T o)
Rules:
• negative -> current < other
• zero -> equal
• positive -> current > other
Example:
class Student implements Comparable<Student>{
int age;
public int compareTo(Student s){
return [Link] - [Link];
}
}
13. Comparator
Used for custom sorting.
Defined in [Link] package.
Method:
int compare(T o1, T o2)
Example:
Comparator<Student> byName =
(a,b) -> [Link]([Link]);
[Link](list, byName);
14. Comparable vs Comparator
Comparable:
• Natural sorting
• Class itself modified
• Single sorting logic
Comparator:
• Custom sorting
• External class/lambda
• Multiple sorting strategies
15. Collections Utility Class
sort()
reverse()
shuffle()
swap()
min()
max()
frequency()
binarySearch()
fill()
copy()
disjoint()
16. Arrays Utility Class
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
17. Important Interview Points
• ArrayList allows duplicates and nulls
• HashSet removes duplicates
• TreeSet stores sorted unique elements
• HashMap allows one null key and many null values
• Hashtable allows no null key/value
• Fail-fast iterators throw ConcurrentModificationException
• Concurrent collections are thread-safe
18. Time Complexity Summary
ArrayList: get O(1), add end O(1)
LinkedList: get O(n), add/remove ends O(1)
HashSet: add/remove/search O(1)
TreeSet: add/remove/search O(log n)
HashMap: put/get O(1)
TreeMap: put/get O(log n)
19. Fail-Fast vs Fail-Safe
Fail-Fast:
• ArrayList, HashMap iterators
• Throw ConcurrentModificationException
Fail-Safe:
• CopyOnWriteArrayList
• ConcurrentHashMap
• Work on cloned copy
20. Best Practices
• Program to interfaces (List, Set, Map)
• Use ArrayList by default for lists
• Use HashMap for general key-value storage
• Override equals() and hashCode() for custom objects
• Use Comparable for natural ordering
• Use Comparator for multiple custom orderings