Java EnumSet of() Method

The EnumSet.of() method in Java is used to create an EnumSet containing specified elements. This guide will cover the method's usage with examples, and we will also cover the overloaded versions of the EnumSet.of() method to show the various ways you can create an EnumSet.

Table of Contents

  1. Introduction
  2. of Method Syntax
  3. Examples
    • Basic Usage of of Method
    • Using Overloaded of Methods
  4. Real-World Use Case
    • Example: Managing Selected Days of the Week
  5. Conclusion

Introduction

The EnumSet.of() method is a static factory method of the EnumSet class in Java. It allows you to create an EnumSet containing specified elements. There are several overloaded versions of this method, allowing you to create an EnumSet with one, two, three, four, or more elements.

of() Method Syntax

The syntax for the of method is as follows:

Single Element

public static <E extends Enum<E>> EnumSet<E> of(E e)

Two Elements

public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2)

Three Elements

public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3)

Four Elements

public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4)

Five or More Elements

public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4, E e5, E... rest)

Examples

Basic Usage of of Method

The of method can be used to create an EnumSet with a specified set of elements.

Example

import java.util.EnumSet;

public class EnumSetOfExample {
    // 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> weekdays = EnumSet.of(Day.MONDAY, Day.TUESDAY, Day.WEDNESDAY, Day.THURSDAY, Day.FRIDAY);

        // Printing the EnumSet
        System.out.println("Weekdays: " + weekdays);
    }
}

Output:

Weekdays: [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY]

Using Overloaded of Methods

You can use the overloaded versions of the of method to create an EnumSet with one, two, three, four, or more elements.

Example

import java.util.EnumSet;

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

    public static void main(String[] args) {
        // Creating an EnumSet with one element
        EnumSet<Fruit> oneFruit = EnumSet.of(Fruit.APPLE);
        System.out.println("One fruit: " + oneFruit);

        // Creating an EnumSet with two elements
        EnumSet<Fruit> twoFruits = EnumSet.of(Fruit.APPLE, Fruit.BANANA);
        System.out.println("Two fruits: " + twoFruits);

        // Creating an EnumSet with three elements
        EnumSet<Fruit> threeFruits = EnumSet.of(Fruit.APPLE, Fruit.BANANA, Fruit.ORANGE);
        System.out.println("Three fruits: " + threeFruits);

        // Creating an EnumSet with four elements
        EnumSet<Fruit> fourFruits = EnumSet.of(Fruit.APPLE, Fruit.BANANA, Fruit.ORANGE, Fruit.MANGO);
        System.out.println("Four fruits: " + fourFruits);

        // Creating an EnumSet with five elements
        EnumSet<Fruit> fiveFruits = EnumSet.of(Fruit.APPLE, Fruit.BANANA, Fruit.ORANGE, Fruit.MANGO, Fruit.GRAPE);
        System.out.println("Five fruits: " + fiveFruits);
    }
}

Output:

One fruit: [APPLE]
Two fruits: [APPLE, BANANA]
Three fruits: [APPLE, BANANA, ORANGE]
Four fruits: [APPLE, BANANA, ORANGE, MANGO]
Five fruits: [APPLE, BANANA, ORANGE, MANGO, GRAPE]

Real-World Use Case

Example: Managing Selected Days of the Week

A common real-world use case for EnumSet.of() is managing a set of selected days of the week.

Example

import java.util.EnumSet;

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

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

        // Checking if a specific day is in the set
        boolean isWednesdaySelected = selectedDays.contains(Day.WEDNESDAY);
        System.out.println("Is Wednesday selected? " + isWednesdaySelected);
    }
}

Output:

Selected days: [MONDAY, WEDNESDAY, FRIDAY]
Is Wednesday selected? true

In this example, EnumSet.of() is used to create a set containing selected days of the week, which can then be easily managed and queried.

Conclusion

The EnumSet.of() method in Java provides a way to create an EnumSet containing specified elements. By understanding how to use this method and its overloaded versions, you can efficiently create and manage collections of enum constants. 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