Java EnumSet clear() Method

The EnumSet.clear() method in Java is used to remove all elements from the EnumSet, making it empty. This guide will cover the method's usage with examples, and we will also cover a real-world use case to show how EnumSet.clear() can be used effectively.

Table of Contents

  1. Introduction
  2. clear Method Syntax
  3. Examples
    • Basic Usage of clear Method
    • Clearing an EnumSet with Multiple Elements
  4. Real-World Use Case
    • Example: Resetting Selected Days of the Week
  5. Conclusion

Introduction

The EnumSet.clear() method is a member of the EnumSet class in Java. It allows you to remove all elements from the EnumSet, leaving it empty. This can be useful in scenarios where you need to reset or reinitialize the set.

clear() Method Syntax

The syntax for the clear method is as follows:

public void clear()
  • Parameters: This method does not take any parameters.
  • Returns: This method does not return a value.

Examples

Basic Usage of clear Method

The clear method can be used to remove all elements from an EnumSet.

Example

import java.util.EnumSet;

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

        // Printing the EnumSet before clearing
        System.out.println("EnumSet before clearing: " + days);

        // Clearing the EnumSet
        days.clear();

        // Printing the EnumSet after clearing
        System.out.println("EnumSet after clearing: " + days);
    }
}

Output:

EnumSet before clearing: [MONDAY, WEDNESDAY, FRIDAY]
EnumSet after clearing: []

Clearing an EnumSet with Multiple Elements

You can use the clear method to remove all elements from an EnumSet that contains multiple elements.

Example

import java.util.EnumSet;

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

        // Printing the EnumSet before clearing
        System.out.println("EnumSet before clearing: " + fruits);

        // Clearing the EnumSet
        fruits.clear();

        // Printing the EnumSet after clearing
        System.out.println("EnumSet after clearing: " + fruits);
    }
}

Output:

EnumSet before clearing: [APPLE, BANANA, ORANGE, MANGO]
EnumSet after clearing: []

Real-World Use Case

Example: Resetting Selected Days of the Week

A common real-world use case for EnumSet.clear() is resetting a set of selected days of the week in a scheduling application.

Example

import java.util.EnumSet;

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

        // Printing the selected days before resetting
        System.out.println("Selected days before resetting: " + selectedDays);

        // Clearing the selected days
        selectedDays.clear();

        // Printing the selected days after resetting
        System.out.println("Selected days after resetting: " + selectedDays);
    }
}

Output:

Selected days before resetting: [MONDAY, WEDNESDAY, FRIDAY, SUNDAY]
Selected days after resetting: []

In this example, EnumSet.clear() is used to reset the set of selected days, making it easy to start with a fresh set of days.

Conclusion

The EnumSet.clear() method in Java provides a way to remove all elements from the EnumSet, leaving it empty. By understanding how to use this method, you can efficiently manage collections of enum constants and handle scenarios where you need to reset or reinitialize the set. 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