🎓 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
In this tutorial, we will learn how to make immutable HashMap with an example.
Learn and master Java Collections Framework at Learn Java Collections FrameworkWe will see two examples to make immutable HashMap:
- First, we will see how to create an immutable HashMap using Collections.unmodifiableMap() method with an example.
- Second, we will see how to create an immutable HashMap using Java 9 provided Map.ofEntries() static factory method.
1. Create Immutable HashMap with Collections.unmodifiableMap() Method
Prior to Java 9, we create an immutable HashMap using Collections.unmodifiableMap() method like:
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}
2. Create immutable HashMap with Java 9 Map.ofEntries() factory method
The below example shows how to create an immutable HashMap using Java 9 provided Map.ofEntries() static factory method.
import java.util.HashMap;
import java.util.Map;
public class ImmutableHashMap {
public static void main(String[] args) {
Map < String, String > fruits = new HashMap < String, String > ();
fruits.put("1", "Banana");
fruits.put("2", "Mango");
fruits.put("3", "Apple");
// java 9 with factory methods
Map < String, String > map = Map.ofEntries(
Map.entry("1", "Banana"),
Map.entry("2", "Mango"),
Map.entry("3", "Apple"));
System.out.println(map);
}
}
Output:
{1=Banana, 2=Mango, 3=Apple}
Conclusion
In this tutorial, we have seen how to make immutable HashMap using Collections.unmodifiableMap() method and Java 9 provided Map.ofEntries() static factory method with an example.Collections Examples
- Java LinkedHashMap Example
- Java HashSet Example
- Java LinkedList Example
- Java ArrayList Example
- How To Remove Duplicate Elements From ArrayList In Java?
- Different Ways to Iterate over List, Set, and Map in Java
- Java Comparator Interface Example
- Java Comparable Interface Example
- Java IdentityHashMap Example
- Java WeakHashMap Example
- Java EnumMap Example
- Java CopyOnWriteArraySet Example
- Java EnumSet Class Example
- Guide to Java 8 forEach Method
- Different Ways to Iterate over a List in Java [Snippet]
- Different Ways to Iterate over a Set in Java [Snippet]
- Different Ways to Iterate over a Map in Java [Snippet]
- Iterate over TreeSet in Java Example
- Iterate over LinkedHashSet in Java Example
- Remove First and Last Elements of LinkedList in Java
- Iterate over LinkedList using an Iterator in Java
- Search an Element in an ArrayList in Java
- Iterate over ArrayList using Iterator in Java
- Remove Element from HashSet in Java
- Iterating over a HashSet using Iterator
References
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