Collections unmodifiableCollection() method in Java with Examples - GeeksforGeeks

08 Oct, 2018
The unmodifiableCollection() method of java.util.Collections class is used to return an unmodifiable view of the specified collection. This method allows modules to provide users with “read-only” access to internal collections. Query operations on the returned collection “read through” to the specified collection, and attempts to modify the returned collection, whether direct or via its iterator, result in an UnsupportedOperationException.
The returned collection does not pass the hashCode and equals operations through to the backing collection, but relies on Object’s equals and hashCode methods. This is necessary to preserve the contracts of these operations in the case that the backing collection is a set or a list.
The returned collection will be serializable if the specified collection is serializable.
Syntax:
public static <T> Collection<T> 
    unmodifiableCollection(Collection<? extends T> c)
Parameters: This method takes the collection as a parameter for which an unmodifiable view is to be returned.
Return Value: This method returns an unmodifiable view of the specified collection.
Below are the examples to illustrate the unmodifiableCollection() method
Example 1:
// Java program to demonstrate// unmodifiableCollection() method// for <Character> Value  import java.util.*;  public class GFG1 {    public static void main(String[] argv) throws Exception    {        try {              // creating object of ArrayList<Character>            List<Character> list = new ArrayList<Character>();              // populate the list            list.add('X');            list.add('Y');              // printing the list            System.out.println("Initial list: " + list);              // getting unmodifiable list            // using unmodifiableCollection() method            Collection<Character>                immutablelist = Collections                                    .unmodifiableCollection(list);        }        catch (UnsupportedOperationException e) {              System.out.println("Exception thrown : " + e);        }    }}
Initial list: [X, Y]

Example 2: For UnsupportedOperationException
// Java program to demonstrate// unmodifiableCollection() method// for UnsupportedOperationException  import java.util.*;  public class GFG1 {    public static void main(String[] argv)        throws Exception    {        try {              // creating object of ArrayList<Character>            List<Character> list = new ArrayList<Character>();              // populate the list            list.add('X');            list.add('Y');              // printing the list            System.out.println("Initial list: " + list);              // getting unmodifiable list            // using unmodifiableCollection() method            Collection<Character>                immutablelist = Collections                                    .unmodifiableCollection(list);              // Adding element to new Collection            System.out.println("\nTrying to modify"                               + " the unmodifiableCollection");              immutablelist.add('Z');        }          catch (UnsupportedOperationException e) {            System.out.println("Exception thrown : " + e);        }    }}
Initial list: [X, Y]

Trying to modify the unmodifiableCollection
Exception thrown : java.lang.UnsupportedOperationException

Java - util package
Java-Collections
Java-Functions
Java Programs
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Iterate Over the Characters of a String in Java
How to Get Elements By Index from HashSet in Java?
Java Program to Write into a File
How to Replace a Element in Java ArrayList?
Java Program to Find Sum of Array Elements
Java Program to Read a File to String
Tic-Tac-Toe Game in Java
How to Write Data into Excel Sheet using Java?
Removing last element from ArrayList in Java
Java Program to Write an Array of Strings to the Output Console