How to Make a Collection Read-Only in Java?
The Collections class is used as a data structure to manage the data. We can add, remove, fetch, and update the data in the List, Set, or Map Object. Collections class has default methods for these operations. We can use those methods easily. By default, when we create an object of the Collections class, it will be Both Readable and Writable.
Read-only Collection: To make the object of Collections to Read-Only, we need to restrict an object to add, remove, or update data from it. The only operation is to fetch the data.
Java has different methods for different Collection type like unmodifiableCollection(), unmodifiableMap(), ununmodifiableSet() e.t.c. All the methods are predefined in java.util.Collections class.The unmodifiableCollection() is a generic method to make Read-Only collection. We need to make the reference of Collections class for that. If we have an object of Set Interface, we can use ununmodifiableSet() to make Read-Only.
Example 1: Below code shows how to make a List unmodifiable.
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class GFG {
public static void main(String[] args)
{
List<Integer> numbers = new ArrayList<>();
for ( int i = 1 ; i <= 10 ; i++) {
numbers.add(i);
}
numbers.stream().forEach(System.out::print);
numbers.add( 11 );
numbers.remove( 8 );
numbers.set( 4 , 4 );
System.out.println(
"\nAfter Performing Some Operations" );
numbers.stream().forEach(System.out::print);
System.out.println(
"\nHence By default Collections object is Readable and Writable" );
try {
numbers = Collections.unmodifiableList(numbers);
numbers.remove( 11 );
}
catch (UnsupportedOperationException
unsupportedOperationException) {
System.out.println(
"Exceptions is "
+ unsupportedOperationException);
}
finally {
System.out.println(numbers.get( 3 ));
System.out.println(
"Now list is only Read-Only" );
}
}
}
|
Output
12345678910
After Performing Some Operations
123446781011
Hence By default Collections object is Readable and Writable
Exceptions is java.lang.UnsupportedOperationException
4
Now list is only Read-Only
Above is an example of how to make a list Read-Only. Before making Read-Only we can perform CRUD operations but after making Read-only list, set(), add(), and remove() methods will generate Exceptions. We can now only fetch the data from the list.
Example 2: Below code shows how to make a Set unmodifiable.
Java
import java.util.Set;
import java.util.HashSet;
import java.util.Collections;
class GFG {
public static void main(String[] args)
{
Set<Integer> numbers = new HashSet<Integer>();
for ( int i = 1 ; i <= 5 ; i++) {
numbers.add(i);
}
numbers.stream().forEach(System.out::print);
numbers.remove( 5 );
System.out.println( "\nAfter Performing Operation" );
numbers.stream().forEach(System.out::print);
System.out.println(
"\nSet is also By Default Readable and Writable" );
try {
numbers = Collections.unmodifiableSet(numbers);
numbers.remove( 4 );
}
catch (UnsupportedOperationException
unsupportedOperationException) {
System.out.println(
"Exceptions is "
+ unsupportedOperationException);
}
finally {
System.out.println(numbers.contains( 3 ));
System.out.println( "Now Set is Read-Only" );
}
}
}
|
Output
12345
After Performing Operation
1234
Set is also By Default Readable and Writable
Exceptions is java.lang.UnsupportedOperationException
true
Now Set is Read-Only
In Above Example, We make Set as Read-Only. We can make Collections object Read-Only by using unmodifiableCollection() and to make Map Read-Only we can use unmodifiableMap() method.
Last Updated :
11 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...