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.
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
Initial list: [X, Y] Unmodifiable list: [X, Y]
Example 2: For UnsupportedOperationException
Initial list: [X, Y] Trying to modify the unmodifiablelist Exception thrown : java.lang.UnsupportedOperationException
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.