Collections Framework
Collections Class
Analyze Collections static utility methods, polymorphic algorithms, and wrapper decorators.
Interview: Focuses on distinguish Collection vs Collections, read-only wrapper limits, and synchronized wrappers.
The java.util.Collections class consists exclusively of static methods that operate on or return collections. It provides polymorphic algorithms and wrapper decorators.
Static Algorithms
Provides standard algorithms like sort, binarySearch, reverse, shuffle, and frequency.
Unmodifiable Wrappers
Methods like unmodifiableList return read-only views, throwing UnsupportedOperationException on modifications.
Thread-Safe Wrappers
Methods like synchronizedList wrap unsynchronized collections in synchronized decorators to provide thread-safety.
Unmodifiable Wrappers vs Immutable Collections
It is important to distinguish unmodifiable wrappers from true immutable collections:
- Unmodifiable view wrappers: Created via
Collections.unmodifiableList(parentList). They prevent modifications on the wrapper reference, but modifications to the underlyingparentListare still visible in the wrapper. - Immutable collections: Created via
List.of(...)(Java 9+). These hold copy references that cannot be modified directly or indirectly, ensuring complete immutability.
Common Pitfalls
- Modifying backing lists: Changing a backing list after wrapping it in an unmodifiable list, which updates the view and can break read-only invariants.
- Unsynchronized iteration: Iterating over a synchronized list created by
Collections.synchronizedListwithout external synchronization, causing concurrent modification exceptions.
Best Practices
- Use true immutable collections: Prefer Java 9's factory methods (e.g.
List.of,Set.of) over unmodifiable wrappers to ensure true immutability. - Synchronize iteration blocks: Always synchronize the iteration loop when traversing a synchronized collection:
synchronized(list) { for(T item : list) {...} }.
Interview-Relevant Information
Q1: What is the difference between Collection and Collections?
Answer: Collection is a root interface for standard JCF data structures (like List or Set). Collections is a static utility class that provides algorithms and wrappers to operate on collections.
Q2: Why does iterating over a synchronized collection require external synchronization?
Answer: Synchronized wrappers only synchronize individual method calls (like get or add). Iteration involves multiple calls across loop iterations, which is not atomic and requires external synchronization to prevent concurrent modifications.
Quick Checklist
Can you distinguish Collection from Collections, explain how unmodifiable wrappers differ from immutable collections, and write a thread-safe iteration block? If yes, you understand Collections class.
Use Cases
Wrapping internal domain lists to expose read-only collection views.
Sorting list models using static utility algorithms.
Common Mistakes
Modifying backing lists after wrapping them in unmodifiable views.
Iterating over synchronized collection wrappers without using external synchronization locks.