Java EnumMap containsValue() Method

The EnumMap.containsValue() method in Java is used to check if a specific value is present in the map. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality using enum constants as keys. We will also cover a real-world use case to show how EnumMap.containsValue() can be used effectively.

Table of Contents

  1. Introduction
  2. containsValue Method Syntax
  3. Examples
    • Basic Usage of containsValue Method
    • Handling Non-Existent Values
  4. Real-World Use Case
    • Example: Checking for Task Existence in the Week
  5. Conclusion

Introduction

The EnumMap.containsValue() method is a member of the EnumMap class in Java. It allows you to check if a specific value is present in the map. This method returns true if the map contains one or more keys mapping to the specified value, and false otherwise.

containsValue() Method Syntax

The syntax for the containsValue method is as follows:

public boolean containsValue(Object value)
  • Parameters:
    • value: The value whose presence in this map is to be tested.
  • Returns: true if this map maps one or more keys to the specified value, false otherwise.

Examples

Basic Usage of containsValue Method

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

Example

import java.util.EnumMap;

public class EnumMapContainsValueExample {
    // 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 "Attend meeting" is present in the EnumMap
        boolean hasAttendMeeting = tasks.containsValue("Attend meeting");

        // Printing the result
        System.out.println("Is 'Attend meeting' present? " + hasAttendMeeting);
    }
}

Output:

Is 'Attend meeting' present? true

Handling Non-Existent Values

If the specified value is not present in the EnumMap, the containsValue method returns false.

Example

import java.util.EnumMap;

public class EnumMapNonExistentValueExample {
    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 "Go shopping" is present in the EnumMap
        boolean hasGoShopping = tasks.containsValue("Go shopping");

        // Printing the result
        System.out.println("Is 'Go shopping' present? " + hasGoShopping);
    }
}

Output:

Is 'Go shopping' present? false

Real-World Use Case

Example: Checking for Task Existence in the Week

A common real-world use case for EnumMap.containsValue() is checking if a particular task is scheduled for any 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");
        tasks.put(Day.SATURDAY, "Family time");
        tasks.put(Day.SUNDAY, "Rest day");

        // Checking if "Family time" is scheduled for any day
        boolean hasFamilyTime = tasks.containsValue("Family time");

        // Printing the result
        System.out.println("Is 'Family time' scheduled for any day? " + hasFamilyTime);
    }
}

Output:

Is 'Family time' scheduled for any day? true

In this example, EnumMap.containsValue() is used to check if a specific task is scheduled for any day of the week, making it easy to verify task assignments.

Conclusion

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

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top