Immutable vs Unmodifiable collection [duplicate]

An unmodifiable collection is often a wrapper around a modifiable collection which other code may still have access to. So while you can’t make any changes to it if you only have a reference to the unmodifiable collection, you can’t rely on the contents not changing.

An immutable collection guarantees that nothing can change the collection any more. If it wraps a modifiable collection, it makes sure that no other code has access to that modifiable collection. Note that although no code can change which objects the collection contains references to, the objects themselves may still be mutable – creating an immutable collection of StringBuilder doesn’t somehow “freeze” those objects.

Basically, the difference is about whether other code may be able to change the collection behind your back.

Leave a Comment