Algorithms

Immutable Collections

๐Ÿ“‹ Quick Reference

PropertyValue
TypeCollections that cannot be modified after creation
Thread SafetyInherently thread-safe (no synchronization needed)
MemoryCompact, no defensive copying needed
NullNot allowed in Java 9+ factory methods
Best ForConfiguration, constants, return values, multi-threading
One-liner: Collections that cannot change after creation - thread-safe by design, no synchronization needed.

๐ŸŽฎ Interactive Visualizer

See how immutable collections work and how structural sharing saves memory:

Loading visualizer...
Try these operations:
  1. See that modification throws UnsupportedOperationException
  2. Watch "derived" collections share structure
  3. Compare memory usage with mutable copies
  4. Observe safe sharing between threads

๐Ÿ”ง Creating Immutable Collections

Java 9+ Factory Methods (Preferred)

JAVA(14 lines)
Code
Loading syntax highlighter...

From Existing Collections (Java 10+)

JAVA(15 lines)
Code
Loading syntax highlighter...

Collections.unmodifiable* (View Wrapper)

JAVA(9 lines)
Code
Loading syntax highlighter...

๐Ÿ“Š Comparison of Approaches

MethodTypeNull?ModificationsThread-Safe
List.of()True copyNoThrowsYes
List.copyOf()True copyNoThrowsYes
unmodifiableList()ViewYesView throws, source can changeIf source unchanged
toUnmodifiableList()True copyNoThrowsYes

โœ… 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)
Code
Loading syntax highlighter...

Safe Return Values

JAVA(13 lines)
Code
Loading syntax highlighter...

Builder Pattern with Immutable Result

JAVA(28 lines)
Code
Loading syntax highlighter...

Thread-Safe Sharing

JAVA(13 lines)
Code
Loading syntax highlighter...

โš ๏ธ Common Pitfalls

1. Confusing Unmodifiable with Immutable

JAVA(7 lines)
Code
Loading syntax highlighter...

2. Null Values

JAVA(7 lines)
Code
Loading syntax highlighter...

3. Mutable Elements in Immutable Collection

JAVA(6 lines)
Code
Loading syntax highlighter...

4. Expecting Modification to Work

JAVA(13 lines)
Code
Loading syntax highlighter...

๐Ÿ”„ Immutable vs Unmodifiable vs Mutable

JAVA(14 lines)
Code
Loading syntax highlighter...

๐ŸŽฏ Interview Practice

Test your immutable collections knowledge with 10 curated interview questions:

๐ŸŽค Immutable Collections Interview Mode - Coming Soon


๐ŸŽค Interview Tips

Q: What's the difference between immutable and unmodifiable?
"

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.

Q: Why use immutable collections?
"

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".

Q: Can I have null in List.of()?
"

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.

Q: How do I modify an immutable collection?
"

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


Visualizer: ImmutableCollections from @tomaszjarosz/react-visualizers