Java EnumMap get() Method

The EnumMap.get(Object key) method in Java is used to retrieve the value associated with a 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.get() can be used effectively.

Table of Contents

  1. Introduction
  2. get Method Syntax
  3. Examples
    • Basic Usage of get Method
    • Handling Non-Existent Keys
  4. Real-World Use Case
    • Example: Retrieving Task for a Specific Day
  5. Conclusion

Introduction

The EnumMap.get(Object key) method is a member of the EnumMap class in Java. It allows you to retrieve the value associated with the specified key. If the key is not present in the map, the method returns null.

get() Method Syntax

The syntax for the get method is as follows:

public V get(Object key)
  • Parameters:
    • key: The key whose associated value is to be returned.
  • Returns: The value to which the specified key is mapped, or null if this map contains no mapping for the key.

Examples

Basic Usage of get Method

The get method can be used to retrieve the value associated with a specific key in an EnumMap.

Example

import java.util.EnumMap;

public class EnumMapGetExample {
    // 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");

        // Retrieving the task for MONDAY
        String taskMonday = tasks.get(Day.MONDAY);

        // Printing the task for MONDAY
        System.out.println("Task for Monday: " + taskMonday);
    }
}

Output:

Task for Monday: Go to gym

Handling Non-Existent Keys

If the specified key is not present in the EnumMap, the get method returns null.

Example

import java.util.EnumMap;

public class EnumMapNonExistentKeyExample {
    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");

        // Attempting to retrieve the task for SUNDAY
        String taskSunday = tasks.get(Day.SUNDAY);

        // Printing the result for SUNDAY
        System.out.println("Task for Sunday: " + taskSunday);
    }
}

Output:

Task for Sunday: null

Real-World Use Case

Example: Retrieving Task for a Specific Day

A common real-world use case for EnumMap.get() is retrieving tasks or activities associated with specific days of the week.

Example

import java.util.EnumMap;

public class TaskRetriever {
    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");

        // Retrieving the task for FRIDAY
        String taskFriday = tasks.get(Day.FRIDAY);

        // Printing the task for FRIDAY
        System.out.println("Task for Friday: " + taskFriday);
    }
}

Output:

Task for Friday: Project presentation

In this example, EnumMap.get() is used to retrieve the task associated with a specific day, making it easy to organize and access day-specific tasks.

Conclusion

The EnumMap.get(Object key) method in Java provides a way to retrieve the value associated with a specific key. By understanding how to use this method, you can efficiently manage and access collections of key-value pairs where the keys are enum constants. This method allows you to retrieve entries in an EnumMap, making it a versatile tool for managing data in various scenarios.

Comments