Skip to content

Instantly share code, notes, and snippets.

@2013techsmarts
Last active February 15, 2017 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2013techsmarts/f7f1a0506cc030dd3fda6204442a01c8 to your computer and use it in GitHub Desktop.
Save 2013techsmarts/f7f1a0506cc030dd3fda6204442a01c8 to your computer and use it in GitHub Desktop.
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 (Collections.java:1056)
| at (#6:1)
jshell>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment