Java SE 9: Factory Methods for Immutable Map and Map.Entry With Examples

Post Brief Table of Content:

  • Introduction
  • Java SE 8: Immutable Empty Map
  • Java SE 9: Immutable Empty Map With Map.of()
  • Java SE 8: Immutable Non-Empty Map
  • Java SE 9: Immutable Non-Empty Map With Map.of()
  • Java SE 9: Immutable Map.ofEntries() Utility
  • Java SE 9: Immutable Map.entry() Utility

Introduction

Oracle Corporation is going to release Java New Version: Java SE 9 around March 2017. So, I would like to deliver a series of Posts on Java SE 9 New Features. It is my seventh post in this series.

I have already delivered couple of posts on Java SE 9 New Features. Before going through this post, please read them. It is the continuation of my previous two posts:

In this post, we are going to discuss one more Java SE 9 New Feature: “Factory Methods for Immutable Map and Map.Entry” with some simple and suitable examples.

NOTE:-
All Java SE 9 factory methods for Immutable List, Set, and Map are static utility methods.

Java SE 8: Immutable Empty Map

Now we will see how to create Empty Immutable Map in Java SE 8 and earlier versions. If we want to create an empty Immutable or Unmodifiable Map, we should use Collections class utility method: unmodifiableMap as shown below:

Empty Map Example:-

Test this code with Java SE 9 REPL:

Here we can observe that to create an empty Immutable Map, we need to do lot of stuff and very tedious and verbose steps. Let us see the same thing in Java SE 9 now.

Java SE 9: Immutable Empty Map With Map.of()

To overcome those shortcomings, Java SE 9 has introduced two sets of useful overloaded methods.

In this section, we will discuss first set of methods that is Map.of() utility methods and will pick-up next set of methods in the next section.

If we go through the Java SE 9 Map API, we can find the below utility methods in Map interface.
Empty Map API Utility

This utility method is used to create an empty Immutable Map that is Map with zero element in Java SE 9.

Empty Map Example:-

Here we can observe that creating an empty Immutable Map in Java SE 9 is very easy and simple process.

Java SE 8: Immutable Non-Empty Map

In this section, we will see how to create a non-empty Immutable Map in Java SE 8 and Earlier versions.

We can use same Collections.unmodifiableMap utility method to create non-empty Immutable Map as shown below.

Non-empty Map Example:-

Here also we can observe that to create a non-empty Immutable Map, we need to do lot of stuff and very tedious and verbose steps. Let us see the same thing in Java SE 9 now.

Test this with Java SE 9 REPL:-

Java SE 9: Immutable Non-Empty Map With Map.of()

In this section, we will discuss about How to use Java SE 9 of() overloaded methods to create Immutable Non-Empty Map. If we go through the Java SE 9 API documentation, we will see the following utility methods in Map interface.

Non-Empty Map API Utility

These useful methods are used to create a new Non-Empty Immutable Map with one element to 10 elements.

Non-Empty Map API Utility

Non-Empty Map Example:-

Here we can observe that it is very easy to create an Empty and Non-empty Immutable Maps in Java SE 9.

Java SE 9: Immutable Map.ofEntries() Utility

As we know, Java SE 9 has introduced two sets of Map Utility methods to create Immutable Map: of and ofEntries. We have already discussed about Map.of() methods in the previous sections. In this section, we will discuss about “How to use Map.ofEntries() methods” to create empty or non-empty Maps.

Map.ofEntries() methods are used to create Immutable or Unmodifiable Maps using Entries.

If we go through the Java SE 9 Map API, we can find the below utility methods in Map interface.
Empty Map API Utility

This var-args method is useful to create an Immutable Map with zero or one or more elements using Entries. We should use Map.entry() utility method to create Entries. Please refer next section to understand this Map.entry() method.

Empty Map Example:-

This useful method is used to create a new Immutable Map with zero element using Entries.

Non-Empty Map Example:-

This useful method is used to create a new Immutable Map with one or more elements using Entries.

Java SE 9: Immutable Map.entry() Utility

As we discussed in the previous section, we can use this Map.entry() Utility method to create an Immutable Map.Entry using given Key and value pairs. It is used as part of Map.ofEntries() method to create an Immutable Map as shown in the above example.

Map.entry() method API:-

Map.entry() method Example:-

Here we have created three Immutable Map.Entry objects. Using these Entry objects, we can create an Immutable Map as shown below:

NOTE:-
Characteristics of a Immutable Map are similar to Immutable List. You can find those information in detail here: Java SE 9: Factory Methods for Immutable List.

That’s it all about “Java SE 9: Factory Methods for Immutable Map and Map.Entry” concept. We will discuss some more Java SE 9 New Features in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions/type errors.

Thank you for reading my tutorials.

Happy Java SE 9 Learning!

By admin

Leave a Reply

%d bloggers like this: