Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Demonstration of Collections.unmodifiableList method
jshell> List<String> list = new ArrayList<String>();
list ==> []
jshell> list.add("Smart");
$2 ==> true
jshell> list.add("Techie");
$3 ==> true
jshell> System.out.println("The list values are: "+ list);
The list values are: [Smart, Techie]
jshell> // make the list unmodifiable
jshell> List<String> immutablelist = Collections.unmodifiableList(list);
immutablelist ==> [Smart, Techie]
jshell> // try to modify the list
jshell> immutablelist.add("Smart_1");
| java.lang.UnsupportedOperationException thrown:
| at Collections$UnmodifiableCollection.add (
| at (#6:1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment