How to create an Immutable List, Set, and Map in Java?

A Java 9 feature which every Java developer should know

javinpaul
Javarevisited
6 min readJul 2, 2020

--

How to create an Immutable List, Set, and Map in Java?

Hello guys, today, I am going to share bout my favorite Java 9 feature “factory methods for collection”, which is introduced as part of JEP 269.

If you have worked in Groovy or Kotlin, then you know that how easy is to create a list with elements using collection literals, like to create a list of 1, 2, 3, you can simply write val items = listOf(1, 2, 3).

Unfortunately, Java doesn’t support that yet, but things have been improved with factory methods for collection in JDK 9, and it’s almost like that. The JDK has added static factory methods like of() onto basic Collection interfaces, which you can use to create a list of items.

It allows you to create a list, set, and a map of values in just one line, just like you can do in Kotlin, Scala, or Groovy:

But the only catch is that you can create an unmodifiable or immutable List, Set, or Map.

The List, Set, or Map returned by the of() static factory method is structurally immutable, which means you cannot add, remove, or change elements once added.

Calling any mutator method will always cause an UnsupportedOperationException to be thrown. However, if the contained elements are themselves mutable, this may cause the Collection to behave inconsistently or its contents to appear to change.

Btw, if you are not familiar with new API Changes added on Java 9 and recent release then I suggest you first go through a comprehensive and up-to-date Java course like The Complete Java MasterClass on Udemy.

It covers all important topics for Java developer and also regularly updated to the latest Java versions. It’s also very cheap and you can get it in just $10 on several Udemy flash sales which happens all around the year.

How to Create Immutable List, Set, and Map in Java 8 and before?

This is the same as the unmodifiable list you create in JDK 6 or 7 as shown below:

The list returned by the unmodifiableList() the method also doesn’t support add, remove, or set operations and throws an UnsupportedOperationException if you call them.

The only difference between two code snippets is that earlier, it required more than 6 lines of code to create an immutable Collection, like an immutable List, Set, or Map, but now you can do that in just one line.

There are also several overloaded versions of List.of() is available on the List interface, like to allow you to create an immutable list of 1–10 elements and a variable argument method, which allows you to create the list of any number of elements.

The same is true for the Set.of() and Map.of() methods as well. Here is an example of creating an immutable Set in Java 9:

You can see that you can create an immutable Set in just one line. Similarly, to create immutable Maps, JDK 9 provides two methods — Map.of(K k1, V v1) and Map.ofEntries(). By using these two, you can create a Map of immutable entries (see What’s New in Java 9 By Sander Mak) to learn more about this.

Immutable List, Set, and Map in Java with Example

This method is overloaded to create a map of up to 10 key-value pairs, but if you need a bigger map with more mapping, then you should use the Map.ofEntries() method.

By the way, do you know how this feature is implemented? And why it wasn’t available before?

If you look at the JDK 9 code or Javadocs, then you will find that this feature is implemented by adding static factory methods on key interfaces of the Java Collection framework, like List, Set, and Map.

This wasn’t possible until JDK 8 because adding a method on the interface means breaking all its clients — and static methods were not allowed on the interface. Things changed in Java 8 with the introduction of default and static methods on the interface, which paved the way for evaluating the JDK API.

I hope more similar enchantment will come in the future which makes using JDK API even easier.

Also, the rules that apply to the use of the different collections also apply (as you would expect) when using these factory methods.

So, you cannot pass a duplicate element when you create a Set because a Set doesn’t allow duplicates.

Similarly, you cannot pass duplicate keys when you create a Map because a Map doesn’t allow duplicate keys. If you do, then an IllegalArgumentException will be thrown

Also, you can’t pass a null value to the collection factory method.

Java and Spring Interview Preparation Material

And, if you are preparing for Java Interviews, Before any Java and Spring Developer interview, I always use to read the below resources

Grokking the Java Interview

Grokking the Java Interview: click here

I have personally bought these books to speed up my preparation.

You can get your sample copy here, check the content of it and go for it

Grokking the Java Interview [Free Sample Copy]: click here

If you want to prepare for the Spring Boot interview you follow this consolidated ebook, it also contains microservice questions from spring boot interviews.

Grokking the Spring Boot Interview

You can get your copy here — Grokking the Spring Boot Interview

That’s all about how to create an immutable list, set, and map in Java 9. The static method on collections has really made using the Java Collection API easier, and at least it’s now similar to what Kotlin or Groovy offers. JDK 9 is full of such useful features, and stay tuned for more of such articles.

If you can’t wait, check out What’s New in Java 9 — Modules and more, which provides a nice overview of all JDK 9 features.

Other Articles You May Like to Explore
The Complete Java Developer RoadMap
10 Things Java and Web Developer Should Learn
10 Testing Tools Java Developers Should Know
5 Frameworks Java Developers Should Learn
5 Courses to Learn Big Data and Apache Spark in Java
10 Courses to learn DevOps for Java Developers
10 Books Every Java Programmer Should Read
10 Tools Java Developers uses in their day-to-day work
10 Tips to become a better Java Developer
10 Frameworks full-stack Java developer should learn
My favorite free courses to learn Spring framework
10 Advanced Spring Boot Courses for Java developers

Thanks for reading this post! If you like this article, then please share it with your friends and colleagues. Also, if you have any questions or feedback, then please drop a note in the comments below!

--

--

javinpaul
Javarevisited

I am Java programmer, blogger, working on Java, J2EE, UNIX, FIX Protocol. I share Java tips on http://javarevisited.blogspot.com and http://java67.com