🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
With Java 9, new factory methods are added to List, Set and Map interfaces to create immutable instances. These factory methods are convenience factory methods to create a collection in less verbose and in a concise way.
In this post, I show you how to create an immutable Map using Java 9 provided Map.ofEntries() static factory method.
Create Immutable Map
Map factory methods are the same as List or Set overloaded factory methods. The only difference is that the signatures of the methods take alternating keys and values as arguments. e.g.
static <K,V> Map<K,V> of()
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> ofEntries(Map.Entry<? extends K,? extends V>... entries)
Create Immutable Map Before Java 9 Example
Prior to Java 9, the creation of an immutable Map was some kind of verbose task. For Example:
public class ImmutableMapExample {
public static void main(String[] args) {
// Creating a HashMap
Map < String, Integer > numberMapping = new HashMap < > ();
// Adding key-value pairs to a HashMap
numberMapping.put("One", 1);
numberMapping.put("Two", 2);
numberMapping.put("Three", 3);
Collections.unmodifiableMap(numberMapping);
System.out.println(fruits);
}
}
Output:
{One=1, Two=2, Three=3}
Create Immutable Map Example - Java 9 Map.ofEntries() Method
Let’s take an example of creating an immutable Map in java 9.
package net.javaguides.corejava.java9;
import java.util.Map;
public class ImmutableMapExample {
public static void main(String[] args) {
Map < String, String > fruits = Map.ofEntries(
Map.entry("1", "Banana"),
Map.entry("2", "Apple"),
Map.entry("3", "Mango"),
Map.entry("4", "Orange"));
System.out.println(fruits);
}
}
Output:
{1=Banana, 2=Apple, 3=Mango, 4=Orange}
Related Java 9 Posts
- Java 9 Private Methods in Interface with Examples - Learn how to use private methods in interface with examples.
- Java 9 List.of() Method - Create Immutable List Example - In this post, I show you how to create an immutable list using Java 9 provided List.of() static factory method.
- Java 9 Set.of() Method - Create Immutable Set Example - In this post, I show you how to create an immutable Set using Java 9 provided Set.of() static factory method.
- Java 9 Map.ofEntries() Method - Create Immutable Map Example - In this post, I show you how to create an immutable Map using Java 9 provided Map.ofEntries() static factory method.
- Java 9 - Stream API Improvements with Examples - In this article, we will learn what are the Stream API improvements made in Java 9 with an example.
- Java 9 - Optional Class Improvements with Examples - Learn Optional class improvements with examples.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment