
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How can we create an unmodifiable Map in Java 9?
An unmodifiable Map is one whose keys and values can't be added, removed, or updated once an unmodifiable instance of a map has created. The static factory methods: Map.of() and Map.ofEntries() from Map that provides a convenient way to create unmodifiable maps in Java 9.
An instance of a map created by using Map.of() and Map.ofEntries() methods has the following characteristics.
- The map returned by the factory methods is conventionally immutable. It means that keys and values can't be added, removed, or updated. Calling any mutator method on the map causes an UnsupportedOperationException.
- If the contained keys/values of a map are themselves mutable, it may cause Map to behave in-consistently or its contents to appear to change.
- An immutable map doesn't allow null keys and values. If any attempt to create with null keys or values, then it throws NullPointerException.
- The duplicate keys are rejected at the time of creation itself. Passing duplicate keys to a static factory method causes IllegalArgumentException.
- The immutable maps are serializable if all keys and values are serializable.
- The order of iteration of mappings is unspecified and subject to change.
Syntax
Map.of(k1, v1, k2, v2) Map.ofEntries(entry(k1, v1), entry(k2, v2),...)
Example of Map.of()
import java.util.Map; public class UnmodifiableMapTest { public static void main(String[] args) { Map<String, String> empMap = Map.of("101", "Raja", "102", "Adithya", "103", "Jai", "104", "Chaitanya"); System.out.println("empMap - " + empMap); empMap.put("105", "Vamsi"); // throws UnsupportedOperationException } }
Output
empMap - {104=Chaitanya, 103=Jai, 102=Adithya, 101=Raja} Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.ImmutableCollections.uoe(Unknown Source) at java.base/java.util.ImmutableCollections$AbstractImmutableMap.put(Unknown Source) at UnmodifiableMapTest.main(UnmodifiableMapTest.java:7)
Example of Map.ofEntries()
import java.util.Map; import static java.util.Map.entry; public class UnmodifidMapTest { public static void main(String[] args) { Map<String, String> empMap = Map.ofEntries(entry("101", "Raja"), entry("102", "Adithya"), entry("103", "Jai"), entry("104", "Chaitanya")); System.out.println("empMap - " + empMap); } }
Output
empMap - {102=Adithya, 101=Raja, 104=Chaitanya, 103=Jai}
- Related Articles
- How can we create an unmodifiable Set in Java 9?
- How can we create an unmodifiable List in Java 9?
- How to create an unmodifiable map in Java?
- How can we implement a map in JShell in Java 9?
- How can we create an instance of VarHandle in Java 9?
- How can we create a Service Provider interface in Java 9?
- How can we create a login form in Java?\n
- How do we create an image map in HTML?
- How can we modify an existing module in Java 9?
- How can we import a gson library in JShell in Java 9?\n
- How can we implement an editable JLabel in Java?\n
- Factory method to create Immutable Map in Java SE 9
- Can we create an object for an interface in java?
- How can we map multiple date formats using Jackson in Java?
- How can we get an ID of the running process in Java 9?

Advertisements