DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Designing a Java Connector for Software Integrations
  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Java Virtual Threads and Scaling

Trending

  • Understanding and Mitigating IP Spoofing Attacks
  • Enhancing Security With ZTNA in Hybrid and Multi-Cloud Deployments
  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  • The Role of Functional Programming in Modern Software Development
  1. DZone
  2. Coding
  3. Java
  4. Creating Immutable Sets, Lists, and Maps in Java 9

Creating Immutable Sets, Lists, and Maps in Java 9

Check out JDK 9's static factory methods on Collection interfaces. See what they offer, how to use them, and some general tips for usage.

By 
Javin Paul user avatar
Javin Paul
·
Mar. 02, 18 · Tutorial
Likes (33)
Comment
Save
Tweet
Share
44.1K Views

Join the DZone community and get the full member experience.

Join For Free

Today, you'll learn about 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, e.g. 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:

List<String> list = List.of("Java", "Kotlin", "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.

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

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

List<String> listOfString = new List<>();

listOfString.add("Java");

listOfString.add("Kotlin");

listOfString.add("Groovy");

listOfString.add("Scala");

listOfString = Collections.unmodifiableList(listOfString);


The list returned by the unmodifiableList() 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, e.g. 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, e.g. 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:

Set<Integer> primes = Set.of(2,3,5,7);


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:

Java 9 Example - Factory Methods for Collection - Creating Unmodifiable List, Set, and Map

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, e.g. 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 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. 

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 offer. 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.

Java (programming language)

Published at DZone with permission of Javin Paul, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Designing a Java Connector for Software Integrations
  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Java Virtual Threads and Scaling

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!