Last active
February 15, 2017 17:11
-
-
Save 2013techsmarts/f7f1a0506cc030dd3fda6204442a01c8 to your computer and use it in GitHub Desktop.
Demonstration of Collections.unmodifiableList method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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