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
put
Method Syntax- Examples
- Basic Usage of
put
Method - 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
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
Post a Comment
Leave Comment