Make delicious recipes!

Read-Only Collections

To provide this capability, the Collections class provides six factory methods, one for each of Collection, List, Map, Set, SortedMap, and SortedSet.

  • Collection unmodifiableCollection(Collection collection)

  • List unmodifiableList(List list)

  • Map unmodifiableMap(Map map)

  • Set unmodifiableSet(Set set)

  • SortedMap unmodifiableSortedMap(SortedMap map)

  • SortedSet unmodifiableSortedSet(SortedSet set)

Once you've filled the collection, the best way to make the collection read-only is to replace the original reference with the read-only reference. If you don't replace the original reference, then the collection is not read-only, as you can still use the original reference to modify the collection. The following program demonstrates the proper way to make a collection read-only. In addition, it shows what happens when you try to modify a read-only collection.

public class ReadOnlyExample {
    public static void main(String args[]) {
        Set set = new HashSet();
        set = Collections.unmodifiableSet(set);

When run and the last add() operation is attempted on the read-only set, an UnsupportedOperationException is thrown.

Implementation of Read-Only collections

If you look at the source code of Collection.unmodifiableCollection(), it shows that its implemented by wrapping the given collection in a wrapper object which passes on the read-only function calls to the underlying collection and throws an exception for write operations.

Similarly, Collections class has static classes for UnmodifiableList, UnmodifiableSet etc. Each of these implements the corresponding interface like List, Set etc, acts as a wrapper object and throws exceptions for write methods while allowing read ones.

=> Every time Collections.unmodifiableList() is called, a new wrapper object is created.

Thread-Safe Collections

The key difference between the historical collection classes and the new implementations within the Collections Framework is the new classes are not thread-safe. This was done such that if you don't need the synchronization, you don't use it, and everything works much faster. If, however, you are using a collection in a multithreaded environment, where multiple threads can modify the collection simultaneously, the modifications need to be synchronized. The Collections class provides for the the ability to wrap existing collections into synchronized ones with another set of six methods:

  • Collection synchronizedCollection(Collection collection)

  • List synchronizedList(List list)

  • Map synchronizedMap(Map map)

  • Set synchronizedSet(Set set)

  • SortedMap synchronizedSortedMap(SortedMap map)

  • SortedSet synchronizedSortedSet(SortedSet set)

Unlike when making a collection read-only, you synchronize the collection immediately after creating it. You also must make sure you do not retain a reference to the original collection, or else you can access the collection unsynchronized. The simplest way to make sure you don't retain a reference is to never create one:

Set set = Collection.synchronizedSet(new HashSet());

Making a collection unmodifiable also makes a collection thread-safe, as the collection can't be modified. This avoids the synchronization overhead.

Implementation of thread-safe collections

Collections.synchronizedList() are implemented much like the Collections.unmodifiableList() methods. When synchronizedList() is called, it creates a wrapper class. All functions of this wrapper class are synchronized and each function simply calls the original collection’s function.

Thus, the wrapper class implements the corresponding collection’s interface and makes it synchronized too.

However, this means that the resulting collection is very heavily synchronized and can become a performance bottleneck.

Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Email: (Your email is not shared with anybody)

Facebook comments:

Site Owner: Sachin Goyal