Collections unmodifiableList() method in Java with Examples - GeeksforGeeks

08 Oct, 2018
The unmodifiableList() method of java.util.Collections class is used to return an unmodifiable view of the specified list. This method allows modules to provide users with “read-only” access to internal lists. Query operations on the returned list “read through” to the specified list, and attempts to modify the returned list, whether direct or via its iterator, result in an UnsupportedOperationException.
The returned list will be serializable if the specified list is serializable. Similarly, the returned list will implement RandomAccess if the specified list does.
Syntax:
public static <T> ListT> 
    unmodifiableList(List<? extends T> list)
Parameters: This method takes the list as a parameter for which an unmodifiable view is to be returned.
Return Value: This method returns an unmodifiable view of the specified list.
Below are the examples to illustrate the unmodifiableList() method
Example 1:
// Java program to demonstrate// unmodifiableList() method  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 unmodifiableList() method            List<Character> immutablelist = Collections                                                .unmodifiableList(list);              // printing the list            System.out.println("Unmodifiable list: "                               + immutablelist);        }          catch (UnsupportedOperationException e) {            System.out.println("Exception thrown : " + e);        }    }}
Initial list: [X, Y]
Unmodifiable list: [X, Y]

Example 2: For UnsupportedOperationException
// Java program to demonstrate// unmodifiableList() method  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 unmodifiableList() method            List<Character> immutablelist = Collections                                                .unmodifiableList(list);              // Adding element to new Collection            System.out.println("\nTrying to modify"                               + " the unmodifiablelist");            immutablelist.add('Z');        }          catch (UnsupportedOperationException e) {            System.out.println("Exception thrown : " + e);        }    }}
Initial list: [X, Y]

Trying to modify the unmodifiablelist
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.
Comments
Old Comments
Java Programming Examples
How to Iterate HashMap in Java?
Factory method design pattern in Java
Traverse Through a HashMap in Java
Iterate through List in Java
Java Program to Remove Duplicate Elements From the Array
Java program to count the occurrence of each character in a string using Hashmap
Iterate Over the Characters of a String in Java
Min Heap in Java
How to Get Elements By Index from HashSet in Java?