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

Filed Under: Java

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:-


 Map<Integer,String> emptyMap = new HashMap<>();
 Map<Integer,String> immutableEmptyMap = Collections.unmodifiableMap(emptyMap);

Test this code with Java SE 9 REPL:


jshell> Map<Integer,String> emptyMap = new HashMap<>()
emptyMap ==> {}

jshell> Map<Integer,String> immutableEmptyMap = Collections.unmodifiableMap(emptyMap)
immutableEmptyMap ==> {}

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.

  • Map.of()
  • Map.ofEntries()

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


 static <K,V> Map<K,V> of()

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

Empty Map Example:-


jshell> Map<Integer,String> emptyImmutableMap = Map.of()
emptyImmutableMap ==> {}

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:-


 Map<Integer,String> nonemptyMap = new HashMap<>();
 nonemptyMap.put(1,"one")
 nonemptyMap.put(2,"two")
 nonemptyMap.put(3,"three")
 Map<Integer,String> immutableNonEmptyMap = Collections.unmodifiableMap(nonemptyMap);

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:-



jshell> Map<Integer,String> nonemptyMap = new HashMap<>()
nonemptyMap ==> {}

jshell> nonemptyMap.put(1,"one")
$2 ==> null

jshell> nonemptyMap.put(2,"two")
$3 ==> null

jshell> nonemptyMap.put(3,"three")
$4 ==> null

onemptyMap)<Integer,String> immutableNonEmptyMap = Collections.unmodifiableMap(n
immutableNonEmptyMap ==> {1=one, 2=two, 3=three}

jshell> nonemptyMap
nonemptyMap ==> {1=one, 2=two, 3=three}

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


 static <K,V> Map<K,V> of(K k1, V v1)
 static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2)
 static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)
 static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)
 static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
 static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
 static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)
 static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)
 static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)
 static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)
 static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)

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

Non-Empty Map API Utility


 static <K,V> Map<K,V> of(K k1, V v1)

Non-Empty Map Example:-


jshell> Map<Integer,String> nonemptyImmutableMap = Map.of(1, "one", 2, "two", 3, "three")
nonemptyImmutableMap ==> {2=two, 3=three, 1=one}

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


static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries)

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:-


jshell> Map<Integer,String> emptyImmutableMap = Map.ofEntries()
emptyImmutableMap ==> {}

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

Non-Empty Map Example:-


import static java.util.Map.entry
jshell> Map<Integer,String> emptyImmutableMap = Map.ofEntries(entry(1,"one"),
   ...> entry(2,"two"), entry(3,"three"))
emptyImmutableMap ==> {1=one, 2=two, 3=three}

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:-


static <K,V> Map.Entry<K,V> entry(K k, V v)

Map.entry() method Example:-


jshell> Map.Entry<Integer,String> immutableMapEntry1 = Map.entry(1,"one")
immutableMapEntry1 ==> 1=one

jshell> Map.Entry<Integer,String> immutableMapEntry2 = Map.entry(2,"two")
immutableMapEntry2 ==> 2=two

jshell> Map.Entry<Integer,String> immutableMapEntry3 = Map.entry(3,"three")
immutableMapEntry3 ==> 3=three

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


jshell> Map<Integer,String> immutableMap = Map.ofEntries(immutableMapEntry1,
   ...> immutableMapEntry2, immutableMapEntry3)
immutableMap ==> {3=three, 2=two, 1=one}

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!

Comments

  1. Charan says:

    How to create immutable collection with more than 10 elements in Java 9 ?

  2. arun singh says:

    thanks ,
    but any of these features have not been added to Java 9 yet.

  3. CHINMAYA DORA says:

    Good explanation. Like it.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages