X

Java 9 Immutable Map Example

This example demonstrates how to create immutable Map with the new Java 9 Collections factory methods

With Java 9 release, Oracle is going one step foreword in acquiring working practices form other popular JVM languages like Kotlin, Guava, Scala, etc.  This is done with proper respect to backwards compatibility and consistency of syntax across the JDK.  One of those new Java 9 features is the creation of immutable (unmodifiable) Map.

Java 9 immutable Map example

Prior to Java 9, the creation of immutable Map was some kind of verbose task. For Example, to create an empty immutable Map with pre Java 9 versions we used following code:

package javatutorial.net;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class UnmodifiableMapExample {

	public static void main(String[] args) {
		Map<Integer, String> emptyMap = new HashMap<>();
		Map<Integer, String> immutableEmptyMap = Collections.unmodifiableMap(emptyMap);
	}

}



What is an Immutable Map

Once created immutable objects in Java can not change values. Immutable Maps are no exception to this rule. You can not :

  • keys and values cannot be added, removed, or updated. If you try to do so, this will resolve in java.lang.UnsupportedOperationException
  • modify Map entries. An attempt to do so will cause java.lang.UnsupportedOperationException
  • use null keys and values in the Map. If you try to add a null key or null value to the Map you will end up with a java.lang.NullPointerException

Create Empty Immutable Map in Java 9

To create an empty Map in Java 9, all we need to do is call the factory Map method of(), see the example below

package javatutorial.net;

import java.util.Map;

public class Java9EmptyImmutableMapExample {
	public static void main(String[] args) {
		Map<Integer,String> emptyImmutableMap = Map.of();
	}
}

Create Immutable Map with Entries in Java 9

There are 10 factory methods to create immutable Maps up to 10 key-value pairs (source: Java 9 Map interface Javadoc):

static <K,V> Map<K,V>	of​(K k1, V v1)	
Returns an immutable map containing a single mapping.

static <K,V> Map<K,V>	of​(K k1, V v1, K k2, V v2)	
Returns an immutable map containing two mappings.

static <K,V> Map<K,V>	of​(K k1, V v1, K k2, V v2, K k3, V v3)	
Returns an immutable map containing three mappings.

static <K,V> Map<K,V>	of​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)	
Returns an immutable map containing four mappings.

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)	
Returns an immutable map containing five mappings.

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)	
Returns an immutable map containing six mappings.

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)	
Returns an immutable map containing seven mappings.

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)	
Returns an immutable map containing eight mappings.

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)	
Returns an immutable map containing nine mappings.

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)	
Returns an immutable map containing ten mappings.

Following example creates an immutable Map with 3 entries:

package javatutorial.net;

import java.util.Map;

public class Java9ImmutableMapExample {
	public static void main(String[] args) {
		Map<Integer,String> map = Map.of(1, "A", 2, "B", 3, "C");
	}
}

 

Using Map.ofEntries() Method to Create Immutable Map in Java 9

The Map interface in Java 9 offers anther utility method to create immutable maps – ofEntries . The signature of the method looks like this:

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

This allows us to insert arbitrary number of entries to the Map. Following example creates 3 entries and creates an immutable Map with them with the help of ofEntries() method :

package javatutorial.net;

import java.util.Map;

public class Java9ImmutableMapOfEntriesExample {
	public static void main(String[] args) {
		Map.Entry<Integer,String> e1 = Map.entry(1, "A");
		Map.Entry<Integer,String> e2 = Map.entry(2, "B");
		Map.Entry<Integer,String> e3 = Map.entry(3, "C");
		
		Map<Integer,String> map = Map.ofEntries(e1, e2, e3);
	}
}

You can significantly shorten the code by static importing java.util.Map.entry , see the example below:

package javatutorial.net;

import static java.util.Map.entry;
import java.util.Map;

public class Java9ImmutableMapOfEntriesExample {
	public static void main(String[] args) {
		Map<Integer,String> map = Map.ofEntries(entry(1,"A"), entry(2,"B"), entry(3,"C"));
	}
}

 

Many of the tutorials you will find here are based on the book Mastering Java 9 written by Dr. Edward Lavieri and Peter Verhas. This book will provide you with complete and deep understanding of the new Java 9 concepts and tools.

0 0 votes
Article Rating
filip:
Related Post