Java 9 : Convenience Factory Methods to create immutable Collections

Java 9

                             In this article we will see another JDK 9 feature to create immutable collections. Till Java 8, If we want to create immutable collections we use to call unmodifiableXXX() methods on java.util.Collections class. For example,  To create unmodifiable list, we should write below code.


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>

The above code is too verbose to create a simple unmodifiable List. As Java is adopting functional programming style Java 9 came up with convenience, more compacted factory methods to create unmodifiable collections with JEP 269. Let us see how  that works.

Create Empty List:


jshell> List immutableList = List.of();
immutableList ==> []
//Add an item to the list
jshell> immutableList.add("Smart");
| Warning:
| unchecked call to add(E) as a member of the raw type java.util.List
| immutableList.add("Smart");
| ^————————^
| java.lang.UnsupportedOperationException thrown:
| at ImmutableCollections.uoe (ImmutableCollections.java:70)
| at ImmutableCollections$AbstractImmutableList.add (ImmutableCollections.java:76)
| at (#2:1)

Create Non-Empty List:


jshell> List immutableList = List.of("Smart","Techie");
immutableList ==> [Smart, Techie]
jshell> //add an item to the list
jshell> immutableList.add("Smart_1");
| Warning:
| unchecked call to add(E) as a member of the raw type java.util.List
| immutableList.add("Smart_1");
| ^————————–^
| java.lang.UnsupportedOperationException thrown:
| at ImmutableCollections.uoe (ImmutableCollections.java:70)
| at ImmutableCollections$AbstractImmutableList.add (ImmutableCollections.java:76)
| at (#2:1)
jshell>

Create Non-Empty Map:


jshell> Map immutableMap = Map.of(1,"Smart",2,"Techie");
immutableMap ==> {1=Smart, 2=Techie}
//Get item from Map
jshell> immutableMap.get(1);
$2 ==> "Smart"
//Add item to map
jshell> immutableMap.put(3,"Smart_1");
| Warning:
| unchecked call to put(K,V) as a member of the raw type java.util.Map
| immutableMap.put(3,"Smart_1");
| ^—————————^
| java.lang.UnsupportedOperationException thrown:
| at ImmutableCollections.uoe (ImmutableCollections.java:70)
| at ImmutableCollections$AbstractImmutableMap.put (ImmutableCollections.java:557)
| at (#3:1)
jshell>

If you look at the above Java 9 factory method, the code is simple one liner to create immutable collections. In the coming article we will see another Java 9 feature. Till then, Stay Tuned!!!

Siva Janapati is an Architect with experience in building Cloud Native Microservices architectures, Reactive Systems, Large scale distributed systems, and Serverless Systems. Siva has hands-on in architecture, design, and implementation of scalable systems using Cloud, Java, Go lang, Apache Kafka, Apache Solr, Spring, Spring Boot, Lightbend reactive tech stack, APIGEE edge & on-premise and other open-source, proprietary technologies. Expertise working with and building RESTful, GraphQL APIs. He has successfully delivered multiple applications in retail, telco, and financial services domains. He manages the GitHub(https://github.com/2013techsmarts) where he put the source code of his work related to his blog posts.

Tagged with: ,
Posted in Java
3 comments on “Java 9 : Convenience Factory Methods to create immutable Collections
  1. […] Convenience Factory Methods to create immutable Collections (plus nice use of the REPL to demo) […]

  2. Map.of takes an array of Object? gross… now more than ever java needs a key:value syntax…

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Dzone.com
DZone

DZone MVB

Java Code Geeks
Java Code Geeks
OpenSourceForYou