Immutable Collection unmodifiableCollection()


The collection classes (data structures), we make, are generally modifiable. That is, elements can be added, deleted or replaced at any time. But sometimes, it may be required to have read-only collection classes where any modification to the collection cannot be done. For this, the Collections class (remember, Collection and Collections, both are different) comes with unmodifiableCollection() method. The collection object returned by this method cannot be modified and if tried, the JVM throws UnsupportedOperationException. This helps us to overcome the accidental erasure of elements or addition of further elements.

Following is the method signature.

  • static Collection unmodifiableCollection(Collection col1): Returns an unmodifiable (read-only) version of col1. Any methods like add() or remove(), if applied throws UnsupportedOperationException.

To use with map, list and set, also exists the methods like unmodifiableMap(), unmodifiableSortedMap(), unmodifiableSet(), unmodifiableSortedSet(), unmodifiableList() etc.

Example on Immutable Collection unmodifiableCollection() method usage
import java.util.*;
public class CollectionsUnmodifiable
{
  public static void main(String args[])
  {
    ArrayList firstList = new ArrayList();
    firstList.add(10);      
    firstList.add(20);
    firstList.add(30);      
    firstList.add(40);
    
    List secondList = Collections.unmodifiableList(firstList);
    System.out.println("secondList elements: " + secondList);

    //  secondList.add(50);   // throws UnsupportedOpearationException

    System.out.println("\nfirstList elements before adding 50: " + firstList);
    firstList.add(50);
    System.out.println("firstList elements after adding 50: " + firstList);

    HashSet firstSet = new HashSet();
    firstSet.add("hello");
    firstSet.add("world");
    Set secondSet = Collections.unmodifiableSet(firstSet);
    System.out.println("\nsecondSet elements: " + secondSet);

    //  secondSet.add("sir");  // throws UnsupportedOpearationException
  }
}


Immutable Collection unmodifiableCollection()
Output screen of Immutable Collection unmodifiableCollection()

ArrayList firstList = new ArrayList();
firstList.add(10);

An ArrayList object firstList is created and added with a few elements with add() method inherited from List interface.

List secondList = Collections.unmodifiableList(firstList);

unmodifiableList(firstList) method returns an object of List that is unmodifiable. The secondList contains the same elements of firstList but cannot be modified further. That is, secondList is immutable and on this no operations of addition (add()), deletion (remove()) or replacement (set()) etc. cannot be done.

// secondList.add(50);

The above statement throws UnsupportedOperationException as it is tried to add an element with add() method.

firstList.add(50);

Observe, the original list object firstList is still mutable. That is, all the operation of addition etc. can be done. In the above statement, element 50 is added.

HashSet firstSet = new HashSet();
firstSet.add(“hello”); firstSet.add(“world”);

Let us try another example with unmodifiableCollection() method. Now it is the turn for Set elements. An object of HashSet firstSet is created and added with two elements.

Set secondSet = Collections.unmodifiableSet(firstSet);

unmodifiableSet(firstSet) method returns an unmodifiable Set object secondSet containing the same elements of firstSet. This secondSet is immutable and cannot be changed. You are permitted only to retrieve the elements.

// secondSet.add(“sir”);

The above statement raises UnsupportedOpearationException at runtime as secondSet object is unmodifiable.

Note: unmodifiableCollection() method returns an immutable object on the mutable object. The original list is still mutable. Only the returned list is immutable (not modifiable). Now the programmer can make use of both the collection classes which ever the code demands.

Leave a Comment

Your email address will not be published.