EnumSet in Java with Real-World Example

EnumSet is a specialized Set implementation for use with enum types. It's part of the java.util package. Being a highly efficient, compact alternative to traditional sets, it is designed to provide all of the richness of the traditional Set interface without the time and space overheads./p>

Key Points: 

Optimized for Enums: EnumSet is specifically designed for use with enum types. 

Performance: It's implemented as a bit-vector, which makes it extremely efficient (faster than HashSet). 

Not Thread-Safe: EnumSet itself is not thread-safe. External synchronization is needed if multiple threads modify an enum set concurrently and at least one of the threads modifies it. 

Nulls: EnumSet does not permit the use of null elements.

Java EnumSet Class Methods

import java.util.EnumSet;

public class EnumSetExample {

    // Define an `enum`
    enum Colors {
        RED, GREEN, BLUE, YELLOW, ORANGE;
    }

    public static void main(String[] args) {
        // 1. Create an empty EnumSet
        EnumSet<Colors> colorSet = EnumSet.noneOf(Colors.class);
        System.out.println("1. Empty EnumSet: " + colorSet);

        // 2. Add single elements
        colorSet.add(Colors.RED);
        colorSet.add(Colors.BLUE);
        System.out.println("2. EnumSet after adding RED and BLUE: " + colorSet);

        // 3. Add multiple elements using EnumSet.of()
        EnumSet<Colors> yellowAndOrange = EnumSet.of(Colors.YELLOW, Colors.ORANGE);
        System.out.println("3. EnumSet of YELLOW and ORANGE: " + yellowAndOrange);

        // 4. Create an EnumSet of all elements
        EnumSet<Colors> allColors = EnumSet.allOf(Colors.class);
        System.out.println("4. EnumSet of all colors: " + allColors);

        // 5. Remove an element
        allColors.remove(Colors.GREEN);
        System.out.println("5. EnumSet after removing GREEN: " + allColors);
    }
}

Output:

1. Empty EnumSet: []
2. EnumSet after adding RED and BLUE: [RED, BLUE]
3. EnumSet of YELLOW and ORANGE: [YELLOW, ORANGE]
4. EnumSet of all colors: [RED, GREEN, BLUE, YELLOW, ORANGE]
5. EnumSet after removing GREEN: [RED, BLUE, YELLOW, ORANGE]

Explanation:

1. Initialized an empty EnumSet using noneOf().

2. Added individual colors to the EnumSet using add().

3. Created an EnumSet with specific elements using of().

4. Created a complete EnumSet with all enum values using allOf().

5. Removed an element from the EnumSet using remove().

Note: EnumSet provides various methods to create and manipulate sets of enum types, making it efficient and type-safe.

Real-World Example: Java EnumSet in Pizza Ordering System

import java.util.EnumSet;

public class PizzaOrderingSystem {

    // Define an `enum` for pizza toppings
    enum Topping {
        CHEESE, TOMATO, PEPPERONI, MUSHROOM, OLIVE, BELL_PEPPER;
    }

    public static void main(String[] args) {
        // 1. A customer orders a pizza with CHEESE, TOMATO, and PEPPERONI
        EnumSet<Topping> pizzaOrder1 = EnumSet.of(Topping.CHEESE, Topping.TOMATO, Topping.PEPPERONI);
        System.out.println("1. Pizza order with toppings: " + pizzaOrder1);

        // 2. Another customer orders a vegetarian pizza
        EnumSet<Topping> vegPizza = EnumSet.of(Topping.CHEESE, Topping.TOMATO, Topping.MUSHROOM, Topping.OLIVE, Topping.BELL_PEPPER);
        System.out.println("2. Vegetarian pizza with toppings: " + vegPizza);

        // 3. Check if a pizza order has a particular topping
        boolean hasMushroom = vegPizza.contains(Topping.MUSHROOM);
        System.out.println("3. Does the vegetarian pizza have mushroom? " + hasMushroom);
    }
}

Output:

1. Pizza order with toppings: [CHEESE, TOMATO, PEPPERONI]
2. Vegetarian pizza with toppings: [CHEESE, TOMATO, MUSHROOM, OLIVE, BELL_PEPPER]
3. Does the vegetarian pizza have mushroom? true

Explanation:

1. Using EnumSet.of(), we can easily create a pizza order with specific toppings.

2. A vegetarian pizza is created by excluding meat toppings from the enum.

3. We can use the contains() method of the EnumSet to check if a specific topping exists in an order.

Using EnumSet in such scenarios allows efficient and type-safe operations, ensuring we don't accidentally include invalid toppings.

Comments