Java Collections unmodifiableCollection() MethodThe unmodifiableCollection() method of Java Collections class is used to get an unmodifiable view of the specified collection. If any attempt occurs to modify the returned collection whether direct or via its iterator, result in an UnsupportedOperationException. SyntaxFollowing is the declaration of unmodifiableCollection() method: Parameter
ReturnsThe unmodifiableCollection() method returns an unmodifiable view of the specified collection. ExceptionsNA Example 1Test it NowOutput: Unmodifiable Collection: [Google, Mozila FireFox, Yahoo] Unmodifiable Collection after adding element to the original one: [Google, Mozila FireFox, Yahoo, Safari] Example 2Test it NowOutput: Initial Collection: [Google, Mozila FireFox, Yahoo] Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1056) at myPackage.UnmodifiableCollectionExample2.main(UnmodifiableCollectionExample2.java:9) Example 3Test it NowOutput: Unmodifiable collection: [10, 20, 30, 40] Unmodifiable collection 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