Unmodifiable Collection in Java
Hello guys! So in this tutorial, we will learn about the unmodifiable collection in Java.
Table of contents:
- Introduction.
- Method
- Syntax
- Example
- Explanation of code
- Conclusion
Unmodifiable Collection In Java
INTRODUCTION
What do you mean by Unmodifiable collection?
A Collection that is incapable of modifications.
Collection can be modifiable as well as unmodifiable.
There are cases where we may require some changes in our Collection later. Likewise, in some cases, we want our collection to be unmodifiable. In short, we want our collection to be permanent. As a result, in this tutorial, we will study the unmodifiable collection in Java.
WE CANNOT PERFORM THE FOLLOWING OPERATIONS USING UNMODIFIABLE COLLECTION:
- ADD
- REMOVE
- CLEAR
METHOD
unmodifiableCollection()
This method is present in java.util.Collections class.
unmodifiableCollection() is used to get an unmodifiable view of the specified collection.
Thus, an attempt to modify the collection by using methods such as add, remove or clear will result in an UnsupportedOperationException.
SYNTAX
public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c)
Where c is the collection for which an unmodifiable view is to be returned.
Also check: AbstractCollection in java
EXAMPLE
For example, we will consider this code:
package javaapplication21; import java.util.*; public class JavaApplication21 { public static void main(String[] args) { List<Object> abc = new ArrayList<Object>(); //isEmpty() METHOD System.out.println("Is the List empty? " + abc.isEmpty()); //size() METHOD System.out.println("The size of the List before adding the obejcts: " + abc.size()); //add() METHOD abc.add(1); abc.add(2); abc.add(3); abc.add(4); abc.add(5); // Displaying the Collection System.out.println("List: " + abc); System.out.println("Is the collection empty? " + abc.isEmpty()); System.out.println("The size of the Collection after adding the obejcts: " + abc.size()); //contains() METHOD System.out.println("Is 5 present in the collection?"+abc.contains(5)); //remove() METHOD abc.remove(3); abc.remove(1); System.out.println("List after removing the objects: " + abc); //clear() METHOD abc.clear(); System.out.println("Is the collection empty? " + abc.isEmpty()); //addition for modifiable list abc.add(1); abc.add(2); abc.add(3); abc.add(4); abc.add(5); System.out.println("Initial List: " + abc); //making the list unmodifiable. Collection<Object> imm_list = Collections.unmodifiableCollection(abc); //Now let's try to modify the list after using unmodifiableCollection() imm_list.add('6'); } }
Output:
The size of the Collection after adding the objects: 5 Is 5 present in the collection? true List after removing the objects: [1, 3, 5] Is the collection empty? true Initial List: [1, 2, 3, 4, 5] Exception in thread "main" java.lang.UnsupportedOperationException
EXPLANATION
So, in the above example, we have created an ArrayList. After creation, we have performed some operations on the list such as, add(), remove(), size(), isEmpty().
Later, we have transformed our list into an unmodifiable list by using unmodifiableCollection().
Thus, when we try to modify the list, an exception ‘ UnsupportedOperationException’ is raised.
CONCLUSION
Hence, we have learned to transform a normal collection into an unmodifiable collection by using unmodifiableCollection() of Java collections.
Leave a Reply