The Collections API is a set of interfaces, classes, and utility methods in java.util package that help store, manipulate, and manage groups of objects efficiently.
The core components include:
-
Interfaces (Collection, List, Set, Queue, Map)
-
Implementations (ArrayList, HashSet, HashMap, etc.)
-
Utility Classes (Collections, Arrays)
-
Algorithms (sorting, searching, shuffling, reversing)
The Collections API standardizes how data structures behave in Java.
There are two main branches:
Iterable
└── Collection
├── List
├── Set
└── Queue
Map
├── HashMap
├── LinkedHashMap
├── TreeMap
└── Hashtable
Collection<E> is the superinterface for List, Set, and Queue.
Common methods:
add(E e)
remove(E e)
size()
clear()
contains(Object o)
isEmpty()
iterator()Implementations:
| Class | Characteristics |
|---|---|
| ArrayList | Dynamic array, fast random access |
| LinkedList | Doubly linked list, fast insertion/deletion |
| Vector | Legacy, synchronized |
| Stack | Legacy LIFO structure |
Implementations:
| Class | Ordering | Null Allowed | Notes |
|---|---|---|---|
| HashSet | No order | Yes | Fastest |
| LinkedHashSet | Insertion order | Yes | Predictable iteration |
| TreeSet | Sorted | No | Uses Red-Black Tree |
Implementations:
| Class | Purpose |
|---|---|
| PriorityQueue | Priority-based retrieval |
| ArrayDeque | Fast stack/queue |
| LinkedList | Can act as Queue or Deque |
Not a child of Collection.
Implementations:
| Class | Ordering | Null Keys | Thread-safe? |
|---|---|---|---|
| HashMap | No | Yes | No |
| LinkedHashMap | Insertion | Yes | No |
| TreeMap | Sorted | No | No |
| Hashtable | No | No | Yes |
| ConcurrentHashMap | No | No | Yes (High performance) |
List<String> list = new ArrayList<>();- Fast for random access
- Slow for insertions (shifts elements)
LinkedList<Integer> ll = new LinkedList<>();- Best for frequent insert/delete
- Slow for random access
Set<Integer> set = new HashSet<>();- Stores unique elements
- Backed by HashMap
Map<Integer, String> map = new HashMap<>();- Key–value storage
- Very fast (O(1) average)
Iterator<String> it = list.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}ListIterator<String> it = list.listIterator();Supports previous(), add(), set().
- Throw ConcurrentModificationException
- Used in: ArrayList, HashMap, HashSet
for(String s : list) {
list.add("x"); // causes ConcurrentModificationException
}- Do NOT throw exception
- Work on clone
- Used in: ConcurrentHashMap, CopyOnWriteArrayList
java.util.Collections provides helper methods.
Collections.sort(list);
Collections.reverse(list);
Collections.shuffle(list);
Collections.max(list);
Collections.min(list);
Collections.binarySearch(list, key);
Collections.synchronizedList(list);Implement inside the class:
class Student implements Comparable<Student> {
public int compareTo(Student s) {
return this.age - s.age;
}
}Comparator<Student> byName = (a, b) -> a.name.compareTo(b.name);
Collections.sort(list, byName);Collections use generics for type safety.
List<String> names = new ArrayList<>();Prevents runtime ClassCastException.
| Feature | ArrayList | LinkedList |
|---|---|---|
| Access | Fast | Slow |
| Insert/delete | Slow | Fast |
| Structure | Array | Linked nodes |
| Feature | HashSet | LinkedHashSet | TreeSet |
|---|---|---|---|
| Order | No | Insertion | Sorted |
| Speed | Fastest | Medium | Slowest |
| Feature | HashMap | LinkedHashMap | TreeMap |
|---|---|---|---|
| Order | None | Insertion | Sorted |
| Speed | Fast | Medium | Slow |
Map<String, Integer> count = new HashMap<>();
for (String word : words) {
count.put(word, count.getOrDefault(word, 0) + 1);
}List<Integer> list = Arrays.asList(5, 2, 8, 1);
Collections.sort(list);PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(30);
pq.add(10);
pq.add(20);
System.out.println(pq.poll()); // 10| Requirement | Use |
|---|---|
| Fast search | HashMap / HashSet |
| Maintain order | ArrayList / LinkedHashMap |
| Remove/add frequently | LinkedList |
| Sorted data | TreeSet / TreeMap |
| Thread-safe list | CopyOnWriteArrayList |
| Thread-safe map | ConcurrentHashMap |
| Queue/stack | ArrayDeque |
-
Collections API = interfaces + classes + algorithms.
-
List, Set, Queue extend Collection, but Map is separate.
-
Choose implementation based on performance and ordering needs.
-
Use
Collectionsutility class for sorting, reversing, searching. -
Understand differences between HashMap, TreeMap, LinkedHashMap.
-
Iterator vs ListIterator vs fail-fast vs fail-safe is important for interviews.
-
Generics give type safety to collections.