Java EnumSet allOf() Method

The EnumSet.allOf() method in Java is used to create an EnumSet containing all of the elements of the specified enum type. This guide will cover the method's usage with examples, and we will also cover a real-world use case to show how EnumSet.allOf() can be used effectively.

Table of Contents

  1. Introduction
  2. allOf Method Syntax
  3. Examples
    • Basic Usage of allOf Method
    • Iterating Over the EnumSet
  4. Real-World Use Case
    • Example: Managing Days of the Week
  5. Conclusion

Introduction

The EnumSet.allOf(Class<E> elementType) method is a static factory method of the EnumSet class in Java. It allows you to create an EnumSet containing all of the elements of the specified enum type.

allOf() Method Syntax

The syntax for the allOf method is as follows:

public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType)
  • Parameters:
    • elementType: The class object of the enum type from which to create the set.
  • Returns: An EnumSet containing all of the elements of the specified enum type.

Examples

Basic Usage of allOf Method

The allOf method can be used to create an EnumSet containing all the elements of a specified enum type.

Example

import java.util.EnumSet;

public class EnumSetAllOfExample {
    // 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 containing all elements of Day enum
        EnumSet<Day> allDays = EnumSet.allOf(Day.class);

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

Output:

All days: [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY]

Iterating Over the EnumSet

You can use the allOf method to create an EnumSet and then iterate over it.

Example

import java.util.EnumSet;

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

    public static void main(String[] args) {
        // Creating an EnumSet containing all elements of Fruit enum
        EnumSet<Fruit> allFruits = EnumSet.allOf(Fruit.class);

        // Iterating over the EnumSet and printing each element
        System.out.println("All fruits:");
        for (Fruit fruit : allFruits) {
            System.out.println(fruit);
        }
    }
}

Output:

All fruits:
APPLE
BANANA
ORANGE
MANGO
GRAPE

Real-World Use Case

Example: Managing Days of the Week

A common real-world use case for EnumSet.allOf() is managing and iterating over all days of the week.

Example

import java.util.EnumSet;

public class WeeklyTaskManager {
    // 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 containing all elements of Day enum
        EnumSet<Day> allDays = EnumSet.allOf(Day.class);

        // Printing a message for each day
        System.out.println("Weekly Task Schedule:");
        for (Day day : allDays) {
            switch (day) {
                case MONDAY:
                    System.out.println(day + ": Start the week with planning.");
                    break;
                case TUESDAY:
                    System.out.println(day + ": Continue with project work.");
                    break;
                case WEDNESDAY:
                    System.out.println(day + ": Mid-week review.");
                    break;
                case THURSDAY:
                    System.out.println(day + ": Focus on team meetings.");
                    break;
                case FRIDAY:
                    System.out.println(day + ": Wrap up the work week.");
                    break;
                case SATURDAY:
                    System.out.println(day + ": Weekend leisure activities.");
                    break;
                case SUNDAY:
                    System.out.println(day + ": Rest and recharge.");
                    break;
            }
        }
    }
}

Output:

Weekly Task Schedule:
MONDAY: Start the week with planning.
TUESDAY: Continue with project work.
WEDNESDAY: Mid-week review.
THURSDAY: Focus on team meetings.
FRIDAY: Wrap up the work week.
SATURDAY: Weekend leisure activities.
SUNDAY: Rest and recharge.

In this example, EnumSet.allOf() is used to create a set containing all days of the week, and then each day is printed with a corresponding message.

Conclusion

The EnumSet.allOf(Class<E> elementType) method in Java provides a way to create an EnumSet containing all elements of a specified enum type. By understanding how to use this method, you can efficiently manage and iterate over all enum constants in a type-safe manner. 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