🎓 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
The EnumMap.put() method in Java is used to associate the specified value with the specified key in the map. This guide will cover the method's usage with examples, and we will also cover a real-world use case to show how EnumMap.put() can be used effectively.
Table of Contents
- Introduction
putMethod Syntax- Examples
- Basic Usage of
putMethod - Updating an Existing Entry
- Basic Usage of
- Real-World Use Case
- Example: Managing Day-specific Tasks
- Conclusion
Introduction
The EnumMap.put() method is a member of the EnumMap class in Java. It allows you to associate a specific value with a specific enum key. If the map previously contained a mapping for the key, the old value is replaced by the specified value.
put() Method Syntax
The syntax for the put method is as follows:
public V put(K key, V value)
- Parameters:
key: The key with which the specified value is to be associated.value: The value to be associated with the specified key.
- Returns: The previous value associated with the key, or
nullif there was no mapping for the key.
Examples
Basic Usage of put Method
The put method can be used to add key-value pairs to an EnumMap.
Example
import java.util.EnumMap;
public class EnumMapPutExample {
// Define an enum representing days of the week
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
// Create an EnumMap with Day as key and String as value
EnumMap<Day, String> tasks = new EnumMap<>(Day.class);
// Adding entries to the EnumMap
tasks.put(Day.MONDAY, "Go to gym");
tasks.put(Day.TUESDAY, "Attend meeting");
tasks.put(Day.WEDNESDAY, "Work from home");
// Printing the EnumMap
System.out.println("Tasks for the week: " + tasks);
}
}
Output:
Tasks for the week: {MONDAY=Go to gym, TUESDAY=Attend meeting, WEDNESDAY=Work from home}
Updating an Existing Entry
If the specified key is already present in the EnumMap, the put method will update the key with the new value.
Example
import java.util.EnumMap;
public class EnumMapUpdateExample {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
// Create an EnumMap with Day as key and String as value
EnumMap<Day, String> tasks = new EnumMap<>(Day.class);
// Adding entries to the EnumMap
tasks.put(Day.MONDAY, "Go to gym");
tasks.put(Day.TUESDAY, "Attend meeting");
// Updating the entry for TUESDAY
tasks.put(Day.TUESDAY, "Client call");
// Printing the EnumMap
System.out.println("Updated tasks for the week: " + tasks);
}
}
Output:
Updated tasks for the week: {MONDAY=Go to gym, TUESDAY=Client call}
Real-World Use Case
Example: Managing Day-specific Tasks
A common real-world use case for EnumMap.put() is managing day-specific tasks for a week.
Example
import java.util.EnumMap;
public class TaskManager {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
// Create an EnumMap to manage tasks for each day
EnumMap<Day, String> tasks = new EnumMap<>(Day.class);
// Adding tasks for each day
tasks.put(Day.MONDAY, "Go to gym");
tasks.put(Day.TUESDAY, "Attend meeting");
tasks.put(Day.WEDNESDAY, "Work from home");
tasks.put(Day.THURSDAY, "Team lunch");
tasks.put(Day.FRIDAY, "Project presentation");
tasks.put(Day.SATURDAY, "Family time");
tasks.put(Day.SUNDAY, "Rest day");
// Printing the tasks for the week
for (Day day : Day.values()) {
System.out.println(day + ": " + tasks.get(day));
}
}
}
Output:
MONDAY: Go to gym
TUESDAY: Attend meeting
WEDNESDAY: Work from home
THURSDAY: Team lunch
FRIDAY: Project presentation
SATURDAY: Family time
SUNDAY: Rest day
In this example, EnumMap.put() is used to manage tasks associated with each day of the week, making it easy to organize and retrieve day-specific tasks.
Conclusion
The EnumMap.put() method in Java provides a way to associate specific values with specific enum keys. By understanding how to use this method, you can efficiently manage collections of key-value pairs where the keys are enum constants. This method allows you to add and update entries in an EnumMap, making it a versatile tool for managing data in various scenarios.
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
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment