Week 93 — What are unmodifiable collections and how can these be obtained?
Question of the Week #93
What are unmodifiable collections and how can these be obtained?
4 Replies
Unmodifiable collections are collections where mutating operations throw
UnsupportedOperationException
s.
It is possible to get unmodifiable collections using the Collections.unmodifiableList(someList)
, Collections.unmodifiableSet(someSet)
, Collections.unmodifiableMap(someMap)
, Collections.unmodifiableCollection(someCollections)
and similar methods.
These are typically views on other collections meaning that if the original collection is modified, these changes are reflected in the unmodifiable collection view.
On the other hand, immutable collections don't change when the collection used for their constructions are modified. These can be obtained using the List.copyOf(someCollection)
, Map.copyOf(someCollection)
and Set.copyOf(someCollection)
methods:
📖 Sample answer from dan1st
These types of collections can’t be changed or altered at all. To use them someone would code the collection as unmodifiable however the person wanted to view the collection.
Submission from miamason
unmodifiable collection is a wrapper around a modifiable collection which other code may still have access to.
Submission from freakybhuvi
Unmodifiable collections are collections, that can't be modified in any way after creation.
They can be obtained by wrapping an existing collection with one of
Collection.unmodifiable<...>
methods.
Example:
As seen in the above example example, trying to modify the collection in any way will throw an UnsupportedOperationException.
Now, for a practical example.
Let's say you have a class, that needs to hold a list of items.
You want users to be able to retrieve the list at any time, but you want to prevent them from adding anything, or you want to implement custom adding logic.
So:
The above example allows you to precisely control what items can be added to the internal list (in that case allowing only strings starting with "item-").
The user can still retrieve the full list at any time and iterate over it, but they can only modify it with your class's add
method.
Of course you can wrap all kinds of collections with Collections.unmodifiableCollection(Collection)
, and even maps: (Collections.unmodifiableMap(Map)
).
They will all behave the same way, returning the read-only version of the provided collection.⭐ Submission from java.net.defective