The EnumSet.add(E e)
method in Java is used to add a specified element 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.add()
can be used effectively.
Table of Contents
- Introduction
add
Method Syntax- Examples
- Basic Usage of
add
Method - Adding Multiple Elements
- Basic Usage of
- Real-World Use Case
- Example: Managing Selected Days of the Week
- Conclusion
Introduction
The EnumSet.add(E e)
method is a member of the EnumSet
class in Java. It allows you to add a specified element to an EnumSet
. If the element is already present in the set, the call leaves the set unchanged and returns false
.
add() Method Syntax
The syntax for the add
method is as follows:
public boolean add(E e)
- Parameters:
e
: The element to be added to the set.
- Returns:
true
if the set did not already contain the specified element.
Examples
Basic Usage of add
Method
The add
method can be used to add an element to an EnumSet
.
Example
import java.util.EnumSet;
public class EnumSetAddExample {
// 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);
// Adding elements to the EnumSet
days.add(Day.MONDAY);
days.add(Day.WEDNESDAY);
days.add(Day.FRIDAY);
// Printing the EnumSet
System.out.println("EnumSet after adding elements: " + days);
}
}
Output:
EnumSet after adding elements: [MONDAY, WEDNESDAY, FRIDAY]
Adding Multiple Elements
You can add multiple elements to an EnumSet
using repeated calls to the add
method.
Example
import java.util.EnumSet;
public class EnumSetAddMultipleExample {
// 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);
// Adding multiple elements to the EnumSet
fruits.add(Fruit.APPLE);
fruits.add(Fruit.BANANA);
fruits.add(Fruit.ORANGE);
// Printing the EnumSet
System.out.println("EnumSet after adding elements: " + fruits);
}
}
Output:
EnumSet after adding elements: [APPLE, BANANA, ORANGE]
Real-World Use Case
Example: Managing Selected Days of the Week
A common real-world use case for EnumSet.add()
is managing a set of selected days of the week for scheduling or task management.
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 empty EnumSet of Day enum
EnumSet<Day> selectedDays = EnumSet.noneOf(Day.class);
// Adding selected days to the EnumSet
selectedDays.add(Day.MONDAY);
selectedDays.add(Day.WEDNESDAY);
selectedDays.add(Day.FRIDAY);
// Printing the selected days
System.out.println("Selected days: " + selectedDays);
// Adding a new day
selectedDays.add(Day.SUNDAY);
// Printing the updated selected days
System.out.println("Updated selected days: " + selectedDays);
}
}
Output:
Selected days: [MONDAY, WEDNESDAY, FRIDAY]
Updated selected days: [MONDAY, WEDNESDAY, FRIDAY, SUNDAY]
In this example, EnumSet.add()
is used to manage a set of selected days of the week, making it easy to add new days as needed.
Conclusion
The EnumSet.add(E e)
method in Java provides a way to add a specified element 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.