Java Collections unmodifiableList() MethodThe unmodifiableList() method of Java Collections class is used to get an unmodifiable view of the specified list. If any attempt occurs to modify the returned list whether direct or via its iterator, results in an UnsupportedOperationException. SyntaxFollowing is the declaration of unmodifiableList() method: Parameter
ReturnsThe unmodifiableList() method returns an unmodifiable view of the specified list. ExceptionsNA Example 1Test it NowOutput: Unmodifiable List: [Google, Mozila FireFox, Yahoo] Unmodifiable List after adding element to the list:[Google, Mozila FireFox, Yahoo, Safari] Example 2Test it NowOutput: Unmodifiable List: [Google, Mozila FireFox, Yahoo] Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1056) at myPackage.UnmodifiableListExample2.main(UnmodifiableListExample2.java:9) Example 3Test it NowOutput: Unmodifiable list: [10, 20, 30, 40] Unmodifiable list after adding (50): [10, 20, 30, 40, 50] Next TopicJava Collections Class |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India