BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java 9 – Factory methods to create Immutable Map

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In the previous tutorials we learned about the factory methods introduced in Java 9 to create immutable List and immutable Set. In this guide, we will learn how to create immutable Map and Map.Entry by using Java 9 Factory methods.

1. Creating Immutable Map prior to Java 9

Before we see how to create immutable Map using Java 9 factory methods, lets see how we used to create them before Java 9.

1.1 Creating Empty Map before Java 9

We used to use the unmodifiableMap() method of Collections class to create unmodifiable(immutable) Map.

Map<String,String> map = new HashMap<String, String>();
Map<String,String> immutableMap = Collections.unmodifiableMap(map);

Lets test this in JShell.
Creating Empty Map before Java 9

1.2 Creating Non-Empty Map before Java 9

 
Map<String,String> map = new HashMap<String, String>();
map.put("Key1", "Jon");
map.put("Key2", "Steve");
map.put("Key3", "Mia");
map.put("Key4", "Lora");
Map<String,String> immutableMap = Collections.unmodifiableMap(map);

Lets test this in JShell.
Java 9 Factory Methods to create immutable Map

2. Java 9 Factory Methods to create immutable Map

There are couple of useful factory methods introduced in Java 9 to create unmodifiable Map. We will take the same examples that we have seen above to compare how things got easier in Java 9. The lines of code that we had to write prior to Java 9 is reduced drastically with the help of factory methods.

2.1 Java 9 – Immutable Empty Map using Map.of() factory method

Method that we use for empty Map:

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

Example:

Map<String,String> immutableMap = Map.of()

Testing in JShell:

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

2.2 Java 9 – Immutable Non-Empty Map using Map.of(Key, Value….) factory method

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

Example:
To demonstrate the use of Map.of() factory method, we have taken the same example that we have seen above using unmodifiableMap() method. As you can see how simple it is in Java 9. We have reduced the 6 lines of code in a single line.

Map<String, String> immutableMap = 
Map.of("Key1", "Jon", "Key2", "Steve", "Key3", "Mia", "Key4", "Lora")

Testing in JShell:

jshell> Map<String, String> immutableMap = 
Map.of("Key1", "Jon", "Key2", "Steve", "Key3", "Mia", "Key4", "Lora")
immutableMap ==> {Key3=Mia, Key4=Lora, Key1=Jon, Key2=Steve}

What is an immutable Map?

1. Immutable Map doesn’t allow addition, deletion and update of its elements. If you try to perform these operations then the program will throw UnsupportedOperationException.

jshell> Map<String, String> immutableMap = 
Map.of("Key1", "Jon", "Key2", "Steve", "Key3", "Mia", "Key4", "Lora")
immutableMap ==> {Key3=Mia, Key4=Lora, Key1=Jon, Key2=Steve}

jshell> immutableMap.put("Key5", "Chaitanya");
| java.lang.UnsupportedOperationException thrown:
| at ImmutableCollections.uoe (ImmutableCollections.java:71)
| at ImmutableCollections$AbstractImmutableMap.put 
(ImmutableCollections.java:558)
| at (#2:1)

2. They do not allow null elements. Adding null elements will throw the same UnsupportedOperationException.

Top Related Articles:

  1. Java 9 – Factory method to create Immutable List
  2. How to iterate TreeMap in reverse order in Java
  3. java 9 JShell – Working with Methods
  4. Java 9 Features with Examples
  5. java 9 JShell – Working with variables

Tags: Java-9

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap