Java EnumSet removeAll() Method

The EnumSet.removeAll() method in Java is used to remove from the EnumSet all of its elements that are contained in the specified collection. This guide will cover the method's usage with examples, and we will also cover a real-world use case to show how EnumSet.removeAll() can be used effectively.

Table of Contents

  1. Introduction
  2. removeAll Method Syntax
  3. Examples
    • Basic Usage of removeAll Method
    • Removing Multiple Elements from the EnumSet
  4. Real-World Use Case
    • Example: Managing Task Removals for Selected Days
  5. Conclusion

Introduction

The EnumSet.removeAll() method is a member of the EnumSet class in Java. It allows you to remove from the EnumSet all of its elements that are contained in the specified collection. If the collection is modified while the operation is in progress, the behavior is undefined.

removeAll() Method Syntax

The syntax for the removeAll method is as follows:

public boolean removeAll(Collection<?> c)
  • Parameters:
    • c: The collection containing elements to be removed from this set.
  • Returns: true if this set changed as a result of the call.

Examples

Basic Usage of removeAll Method

The removeAll method can be used to remove all elements from the EnumSet that are contained in the specified collection.

Example

import java.util.EnumSet;
import java.util.ArrayList;
import java.util.List;

public class EnumSetRemoveAllExample {
    // Define an enum representing days of the week
    enum Day {
        MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
    }

    public static void main(String[] args) {
        // Creating an EnumSet with specific elements
        EnumSet<Day> days = EnumSet.of(Day.MONDAY, Day.WEDNESDAY, Day.FRIDAY, Day.SUNDAY);

        // Creating a list of days to remove
        List<Day> daysToRemove = new ArrayList<>();
        daysToRemove.add(Day.MONDAY);
        daysToRemove.add(Day.WEDNESDAY);

        // Removing all elements in the list from the EnumSet
        boolean changed = days.removeAll(daysToRemove);

        // Printing the result and the EnumSet
        System.out.println("EnumSet changed: " + changed);
        System.out.println("EnumSet after removal: " + days);
    }
}

Output:

EnumSet changed: true
EnumSet after removal: [FRIDAY, SUNDAY]

Removing Multiple Elements from the EnumSet

You can use the removeAll method to remove multiple elements from the EnumSet using various collections.

Example

import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;

public class EnumSetRemoveAllMultipleExample {
    // Define an enum representing types of fruits
    enum Fruit {
        APPLE, BANANA, ORANGE, MANGO, GRAPE
    }

    public static void main(String[] args) {
        // Creating an EnumSet with specific elements
        EnumSet<Fruit> fruits = EnumSet.of(Fruit.APPLE, Fruit.BANANA, Fruit.ORANGE, Fruit.MANGO);

        // Creating a set of fruits to remove
        Set<Fruit> fruitsToRemove = new HashSet<>();
        fruitsToRemove.add(Fruit.APPLE);
        fruitsToRemove.add(Fruit.MANGO);

        // Removing all elements in the set from the EnumSet
        boolean changed = fruits.removeAll(fruitsToRemove);

        // Printing the result and the EnumSet
        System.out.println("EnumSet changed: " + changed);
        System.out.println("EnumSet after removal: " + fruits);
    }
}

Output:

EnumSet changed: true
EnumSet after removal: [BANANA, ORANGE]

Real-World Use Case

Example: Managing Task Removals for Selected Days

A common real-world use case for EnumSet.removeAll() is managing task removals for selected days of the week in a scheduling application.

Example

import java.util.EnumSet;
import java.util.List;
import java.util.Arrays;

public class TaskRemovalManager {
    // Define an enum representing days of the week
    enum Day {
        MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
    }

    public static void main(String[] args) {
        // Creating an EnumSet with selected days
        EnumSet<Day> selectedDays = EnumSet.of(Day.MONDAY, Day.WEDNESDAY, Day.FRIDAY, Day.SUNDAY);

        // Creating a list of days to remove tasks for
        List<Day> daysToRemove = Arrays.asList(Day.MONDAY, Day.SUNDAY);

        // Removing tasks for the specified days
        boolean changed = selectedDays.removeAll(daysToRemove);

        // Printing the results and the EnumSet
        System.out.println("Tasks removed for specified days: " + changed);
        System.out.println("Selected days after removal: " + selectedDays);
    }
}

Output:

Tasks removed for specified days: true
Selected days after removal: [WEDNESDAY, FRIDAY]

In this example, EnumSet.removeAll() is used to manage task removals for selected days, making it easy to update and verify the selected days.

Conclusion

The EnumSet.removeAll() method in Java provides a way to remove from the EnumSet all of its elements that are contained in the specified collection. By understanding how to use this method, you can efficiently manage collections of enum constants and handle batch removals. This method allows you to utilize the power of EnumSet for various scenarios, making it a versatile tool for managing collections of enum constants.

Comments