Java EnumMap put() Method

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 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 (176K+ 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

  1. Introduction
  2. put Method Syntax
  3. Examples
    • Basic Usage of put Method
    • Updating an Existing Entry
  4. Real-World Use Case
    • Example: Managing Day-specific Tasks
  5. 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 null if 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.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare