Java EnumSet addAll() Method

The EnumSet.addAll(Collection<E> c) method in Java is used to add all elements from a specified collection to an EnumSet. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality using different enum constants. We will also cover a real-world use case to show how EnumSet.addAll() can be used effectively.

Table of Contents

  1. Introduction
  2. addAll Method Syntax
  3. Examples
    • Basic Usage of addAll Method
    • Adding Multiple Elements from a Collection
  4. Real-World Use Case
    • Example: Managing Multiple Sets of Days of the Week
  5. Conclusion

Introduction

The EnumSet.addAll(Collection<E> c) method is a member of the EnumSet class in Java. It allows you to add all elements from a specified collection to an EnumSet. If the specified collection is modified while the operation is in progress, the behavior is undefined.

addAll() Method Syntax

The syntax for the addAll method is as follows:

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

Examples

Basic Usage of addAll Method

The addAll method can be used to add all elements from a specified collection to an EnumSet.

Example

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

public class EnumSetAddAllExample {
    // 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 empty EnumSet of Day enum
        EnumSet<Day> days = EnumSet.noneOf(Day.class);

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

        // Adding all elements from the list to the EnumSet
        days.addAll(daysList);

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

Output:

EnumSet after adding elements: [MONDAY, WEDNESDAY, FRIDAY]

Adding Multiple Elements from a Collection

You can use the addAll method to add multiple elements from various collections to an EnumSet.

Example

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

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

    public static void main(String[] args) {
        // Creating an empty EnumSet of Fruit enum
        EnumSet<Fruit> fruits = EnumSet.noneOf(Fruit.class);

        // Creating a list of fruits
        List<Fruit> fruitList = new ArrayList<>();
        fruitList.add(Fruit.APPLE);
        fruitList.add(Fruit.BANANA);

        // Creating a set of fruits
        Set<Fruit> fruitSet = new HashSet<>();
        fruitSet.add(Fruit.ORANGE);
        fruitSet.add(Fruit.MANGO);

        // Adding all elements from the list and the set to the EnumSet
        fruits.addAll(fruitList);
        fruits.addAll(fruitSet);

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

Output:

EnumSet after adding elements: [APPLE, BANANA, ORANGE, MANGO]

Real-World Use Case

Example: Managing Multiple Sets of Days of the Week

A common real-world use case for EnumSet.addAll() is managing multiple sets of days of the week for different schedules or tasks.

Example

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

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

        // Creating a list of weekend days
        List<Day> weekend = new ArrayList<>();
        weekend.add(Day.SATURDAY);
        weekend.add(Day.SUNDAY);

        // Adding all weekend days to the workdays EnumSet (incorrectly)
        workdays.addAll(weekend);

        // Printing the combined EnumSet
        System.out.println("Combined days (incorrect): " + workdays);

        // Creating a separate EnumSet for work and weekend days
        EnumSet<Day> allDays = EnumSet.noneOf(Day.class);

        // Adding all workdays and weekend days to the allDays EnumSet
        allDays.addAll(workdays);
        allDays.addAll(weekend);

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

Output:

Combined days (incorrect): [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY]
Combined days (correct): [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY]

In this example, EnumSet.addAll() is used to manage multiple sets of days, ensuring that all days are included in the final EnumSet.

Conclusion

The EnumSet.addAll(Collection<E> c) method in Java provides a way to add all elements from a specified collection to an EnumSet. By understanding how to use this method, you can efficiently 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top