Java EnumMap containsKey() Method

The EnumMap.containsKey(Object key) method in Java is used to check if a specific key is present 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.containsKey() can be used effectively.

Table of Contents

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

Introduction

The EnumMap.containsKey(Object key) method is a member of the EnumMap class in Java. It allows you to check if a specific key is present in the map. This method returns true if the map contains a mapping for the specified key, and false otherwise.

containsKey() Method Syntax

The syntax for the containsKey method is as follows:

public boolean containsKey(Object key)
  • Parameters:
    • key: The key whose presence in this map is to be tested.
  • Returns: true if this map contains a mapping for the specified key, false otherwise.

Examples

Basic Usage of containsKey Method

The containsKey method can be used to check if a specific key is present in an EnumMap.

Example

import java.util.EnumMap;

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

        // Checking if WEDNESDAY is present in the EnumMap
        boolean hasWednesday = tasks.containsKey(Day.WEDNESDAY);

        // Printing the result
        System.out.println("Is WEDNESDAY present? " + hasWednesday);
    }
}

Output:

Is WEDNESDAY present? true

Handling Non-Existent Keys

If the specified key is not present in the EnumMap, the containsKey method returns false.

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");

        // Checking if SUNDAY is present in the EnumMap
        boolean hasSunday = tasks.containsKey(Day.SUNDAY);

        // Printing the result
        System.out.println("Is SUNDAY present? " + hasSunday);
    }
}

Output:

Is SUNDAY present? false

Real-World Use Case

Example: Checking Task Assignment for a Specific Day

A common real-world use case for EnumMap.containsKey() is checking if a task has been assigned for a specific day of the week.

Example

import java.util.EnumMap;

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

        // Checking if a task is assigned for SATURDAY
        boolean hasTaskForSaturday = tasks.containsKey(Day.SATURDAY);

        // Printing the result
        System.out.println("Is there a task assigned for SATURDAY? " + hasTaskForSaturday);
    }
}

Output:

Is there a task assigned for SATURDAY? false

In this example, EnumMap.containsKey() is used to check if a task has been assigned for a specific day, making it easy to verify task assignments.

Conclusion

The EnumMap.containsKey(Object key) method in Java provides a way to check if a specific key is present in the map. By understanding how to use this method, you can efficiently manage and verify the presence of keys in collections where the keys are enum constants. This method allows you to check for the existence of entries in an EnumMap, making it a versatile tool for managing data in various scenarios.

Comments