Immutable Collections
๐ Quick Reference
| Property | Value |
|---|---|
| Type | Collections that cannot be modified after creation |
| Thread Safety | Inherently thread-safe (no synchronization needed) |
| Memory | Compact, no defensive copying needed |
| Null | Not allowed in Java 9+ factory methods |
| Best For | Configuration, constants, return values, multi-threading |
๐ฎ Interactive Visualizer
See how immutable collections work and how structural sharing saves memory:
Loading visualizer...
- See that modification throws UnsupportedOperationException
- Watch "derived" collections share structure
- Compare memory usage with mutable copies
- Observe safe sharing between threads
๐ง Creating Immutable Collections
Java 9+ Factory Methods (Preferred)
JAVA(14 lines)CodeLoading syntax highlighter...
From Existing Collections (Java 10+)
JAVA(15 lines)CodeLoading syntax highlighter...
Collections.unmodifiable* (View Wrapper)
JAVA(9 lines)CodeLoading syntax highlighter...
๐ Comparison of Approaches
| Method | Type | Null? | Modifications | Thread-Safe |
|---|---|---|---|---|
List.of() | True copy | No | Throws | Yes |
List.copyOf() | True copy | No | Throws | Yes |
unmodifiableList() | View | Yes | View throws, source can change | If source unchanged |
toUnmodifiableList() | True copy | No | Throws | Yes |
โ When to Use Immutable Collections
Good Use Cases
- Constants - application-wide shared values
- Configuration - settings that shouldn't change
- Method returns - caller can't modify your internal state
- Multi-threading - no synchronization needed
- Defensive copies - no need to copy if immutable
- Map keys - guaranteed hashCode stability
Avoid When
- Need to modify - obvious but important
- Building incrementally - use builder then convert
- Need null values - Java 9+ factory methods reject null
๐งฉ Common Patterns
Configuration Constants
JAVA(12 lines)CodeLoading syntax highlighter...
Safe Return Values
JAVA(13 lines)CodeLoading syntax highlighter...
Builder Pattern with Immutable Result
JAVA(28 lines)CodeLoading syntax highlighter...
Thread-Safe Sharing
JAVA(13 lines)CodeLoading syntax highlighter...
โ ๏ธ Common Pitfalls
1. Confusing Unmodifiable with Immutable
JAVA(7 lines)CodeLoading syntax highlighter...
2. Null Values
JAVA(7 lines)CodeLoading syntax highlighter...
3. Mutable Elements in Immutable Collection
JAVA(6 lines)CodeLoading syntax highlighter...
4. Expecting Modification to Work
JAVA(13 lines)CodeLoading syntax highlighter...
๐ Immutable vs Unmodifiable vs Mutable
JAVA(14 lines)CodeLoading syntax highlighter...
๐ฏ Interview Practice
Test your immutable collections knowledge with 10 curated interview questions:
๐ค Immutable Collections Interview Mode - Coming Soon
๐ค Interview Tips
"Unmodifiable is a view wrapper that prevents direct modification but the underlying collection can still change. Immutable collections truly cannot change - there's no underlying mutable collection to modify.
"Thread safety without synchronization, no defensive copies needed, can be safely shared and cached, hashCode stability for map keys, and they communicate intent - "this won't change".
"No, Java 9+ factory methods throw NullPointerException for null elements. This is by design - nulls in collections are often bugs. Use Collections.unmodifiableList() if you really need nulls.
"You create a new collection with the modification. Use streams or builders to derive new collections from existing ones. This is the functional programming approach.
๐ Series Navigation
ImmutableCollections from @tomaszjarosz/react-visualizers