Java EnumMap remove() Method

The EnumMap.remove(Object key, Object value) method in Java is used to remove the entry for a specified key only if it is currently mapped to a specified value. This guide will cover the method's usage with examples, and we will also cover a real-world use case to show how EnumMap.remove() can be used effectively.

Table of Contents

  1. Introduction
  2. remove Method Syntax
  3. Examples
    • Basic Usage of remove Method
    • Handling Non-Matching Values
  4. Real-World Use Case
    • Example: Removing Specific Task Assignments
  5. Conclusion

Introduction

The EnumMap.remove(Object key, Object value) method is a member of the EnumMap class in Java. It allows you to remove a key-value pair from the map only if the specified key is currently mapped to the specified value.

remove() Method Syntax

The syntax for the remove method is as follows:

public boolean remove(Object key, Object value)
  • Parameters:
    • key: The key whose entry is to be removed.
    • value: The value expected to be associated with the specified key.
  • Returns: true if the entry was removed, false otherwise.

Examples

Basic Usage of remove Method

The remove method can be used to remove a key-value pair from an EnumMap if the key is currently mapped to the specified value.

Example

import java.util.EnumMap;

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

        // Removing an entry only if it matches the specified value
        boolean removed = tasks.remove(Day.MONDAY, "Go to gym");

        // Printing the result
        System.out.println("Entry removed: " + removed);
        tasks.forEach((day, task) -> System.out.println(day + ": " + task));
    }
}

Output:

Entry removed: true
TUESDAY: Attend meeting
WEDNESDAY: Work from home

Handling Non-Matching Values

If the value does not match the current value associated with the key, the remove method returns false and does not remove the entry.

Example

import java.util.EnumMap;

public class EnumMapNonMatchingValueExample {
    // Define an enum representing months of the year
    enum Month {
        JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
    }

    public static void main(String[] args) {
        // Create an EnumMap with Month as key and String as value
        EnumMap<Month, String> holidays = new EnumMap<>(Month.class);

        // Adding entries to the EnumMap
        holidays.put(Month.JANUARY, "New Year's Day");
        holidays.put(Month.DECEMBER, "Christmas Day");

        // Attempting to remove an entry with a non-matching value
        boolean removed = holidays.remove(Month.JANUARY, "Republic Day");

        // Printing the result
        System.out.println("Entry removed: " + removed);
        holidays.forEach((month, holiday) -> System.out.println(month + ": " + holiday));
    }
}

Output:

Entry removed: false
JANUARY: New Year's Day
DECEMBER: Christmas Day

Real-World Use Case

Example: Removing Specific Task Assignments

A common real-world use case for EnumMap.remove() is removing specific task assignments for a day only if the task matches a given value.

Example

import java.util.EnumMap;

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

        // Removing specific task assignment for a day
        boolean removed = tasks.remove(Day.THURSDAY, "Team lunch");

        // Printing the result
        System.out.println("Task for Thursday removed: " + removed);
        tasks.forEach((day, task) -> System.out.println("Task for " + day + ": " + task));
    }
}

Output:

Task for Thursday removed: true
Task for MONDAY: Go to gym
Task for TUESDAY: Attend meeting
Task for WEDNESDAY: Work from home
Task for FRIDAY: Project presentation
Task for SATURDAY: Family time
Task for SUNDAY: Rest day

In this example, EnumMap.remove() is used to remove a specific task assignment for a day only if the task matches the given value, ensuring that only the correct task is removed.

Conclusion

The EnumMap.remove() method in Java provides a way to remove an entry for a specified key only if it is currently mapped to a specified value. By understanding how to use this method, you can efficiently manage and remove specific key-value pairs in collections where the keys are enum constants. This method allows you to manage and utilize the entries in an EnumMap, making it a versatile tool for managing data in various scenarios.

Comments