Java EnumSet addAll() Method

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

The EnumSet.addAll() method in Java is used to add all elements from a specified collection to an EnumSet. This guide will cover the method's usage with examples, and 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() 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() 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.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare