Java 9 Immutable Collections Example

Date: 2017-07-05
Java 9 introduced convenient factory methods for creating immutable collections (List, Set, Map). Prior to Java 9, creating immutable collections required more verbose code. These new methods, like List.of()
, Set.of()
, and Map.of()
, provide concise ways to create small, unmodifiable collections. While convenient, it's crucial to remember that these collections, despite being immutable, still possess methods like add()
and put()
, which will throw UnsupportedOperationException
if called. These methods offer a cleaner syntax but require careful handling to avoid unexpected exceptions. Read more: https://examples.javacodegeeks.com/core-java/java-9-immutable-collections-example/
Subscribe to my newsletter
Read articles from Yatin B. directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
