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:
- Java – How to make List or ArrayList unmodifiable List or read-only ?
- Java – How to make Set or HashSet unmodifiable Set or read-only ?
- Java – How to make Map or HashMap unmodifiable Map or read-only ?
- Java – How to make any Collection class as unmodifiable Collection or read-only ?
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#unmodifiableCollection-java.util.Collection-
- https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Set.html
- https://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html
Happy Coding !!
Happy Learning !!