Java – How to make a Collection read-only or unmodifiable ?

In this article, we will discuss how to convert a modifiable Collection into an unmodifiable Collection using Collections.unmodifiableCollection(); method

1. Collection implemented classes:

Classes that implement directly or via Collection are,

  • ArrayList
  • LinkedList
  • Vector
  • Stack
  • HashSet
  • LinkedHashSet
  • TreeSet
  • PriorityQueue
  • PriorityBlockingQueue
  • LinkedBlockingQueue

2. Convert Collection into Read-only:

  • For this illustration, we can use any of the Collection implemented classes like ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, etc.
  • For some business purpose/requirement, Collection needs to be made, 
    • read-only
    • immutable
    • unmodifiable
  • To achieve this, use one of the utility method called unmodifiableCollection() from Collections class
  • Syntax: Collections.unmodifiableCollection(modifiableCollection); i.e.; pass modifiable Collection as input-argument
  • Trying to add/remove/modify/update an unmodifiable Collection throws UnsupportedOperationException as shown in the below illustration
  • Note: however, adding/deleting/updating elements in the original Collection is still possible

ConvertCollectionInToReadOnly.java

package in.bench.resources.unmodifiable.collection;

import java.util.Collection;
import java.util.Collections;
import java.util.TreeSet;

public class ConvertCollectionInToReadOnly {

	public static void main(String[] args) {

		// 1. Collection of String
		Collection<String> treeSet = new TreeSet<>();


		// 1.1 add names
		treeSet.add("Woman");
		treeSet.add("Batman");
		treeSet.add("Superman");
		treeSet.add("Spiderman");
		treeSet.add("Human");


		// 1.2 iterating/printing original Collection
		System.out.println("1. Orginal Collection : \n");
		treeSet.forEach(System.out::println);


		// 2. convert modifiable list to immutable list
		Collection<String> unmodifiableCollection = Collections
				.unmodifiableCollection(treeSet);


		// 2.1 iterating/printing original unmodifiableCollection
		System.out.println("\n\n2. Read-only Collection : \n");
		unmodifiableCollection.forEach(System.out::println);


		// 3. trying to modify unmodifiable- Collection
		System.out.println("\n\n3. Trying to modify unmodifiable Collection : \n");
		try {
			unmodifiableCollection.add("Newman");
		}
		catch(UnsupportedOperationException usopex) {
			System.out.println("In Exception block : ");
			usopex.printStackTrace();
		}
	}
}

Output:

1. Orginal Collection : 

Batman
Human
Spiderman
Superman
Woman


2. Read-only Collection : 

Batman
Human
Spiderman
Superman
Woman


3. Trying to modify unmodifiable Collection : 

In Exception block : 
java.lang.UnsupportedOperationException
	at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1067)
	at in.bench.resources.unmodifiable.collection.ConvertCollectionInToReadOnly
.main(ConvertCollectionInToReadOnly.java:41)

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – Find Longest String in an Arrays or List or Stream ?
Java 8 – Remove input-ted Character from a given String