The EnumSet.containsAll(Collection<?> c)
method in Java is used to check if the EnumSet
contains all elements of a specified collection. 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.containsAll()
can be used effectively.
Table of Contents
- Introduction
containsAll
Method Syntax- Examples
- Basic Usage of
containsAll
Method - Checking for All Elements in a Collection
- Basic Usage of
- Real-World Use Case
- Example: Verifying Selected Days of the Week
- Conclusion
Introduction
The EnumSet.containsAll(Collection<?> c)
method is a member of the EnumSet
class in Java. It allows you to check if the EnumSet
contains all elements of a specified collection. The method returns true
if all elements are present, and false
otherwise.
containsAll() Method Syntax
The syntax for the containsAll
method is as follows:
public boolean containsAll(Collection<?> c)
- Parameters:
c
: The collection to be checked for containment in this set.
- Returns:
true
if this set contains all of the elements in the specified collection.
Examples
Basic Usage of containsAll
Method
The containsAll
method can be used to check if the EnumSet
contains all elements of a specified collection.
Example
import java.util.EnumSet;
import java.util.ArrayList;
import java.util.List;
public class EnumSetContainsAllExample {
// 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> days = EnumSet.of(Day.MONDAY, Day.WEDNESDAY, Day.FRIDAY);
// Creating a list of days to check
List<Day> daysToCheck = new ArrayList<>();
daysToCheck.add(Day.MONDAY);
daysToCheck.add(Day.WEDNESDAY);
// Checking if the EnumSet contains all elements of the list
boolean containsAll = days.containsAll(daysToCheck);
// Printing the result
System.out.println("EnumSet contains all elements of the list: " + containsAll);
}
}
Output:
EnumSet contains all elements of the list: true
Checking for All Elements in a Collection
You can use the containsAll
method to check if the EnumSet
contains all elements of various collections.
Example
import java.util.EnumSet;
import java.util.ArrayList;
import java.util.List;
public class EnumSetContainsAllMultipleExample {
// Define an enum representing types of fruits
enum Fruit {
APPLE, BANANA, ORANGE, MANGO, GRAPE
}
public static void main(String[] args) {
// Creating an EnumSet with specific elements
EnumSet<Fruit> fruits = EnumSet.of(Fruit.APPLE, Fruit.BANANA, Fruit.ORANGE);
// Creating a list of fruits to check
List<Fruit> fruitsToCheck1 = new ArrayList<>();
fruitsToCheck1.add(Fruit.APPLE);
fruitsToCheck1.add(Fruit.BANANA);
// Creating another list of fruits to check
List<Fruit> fruitsToCheck2 = new ArrayList<>();
fruitsToCheck2.add(Fruit.MANGO);
fruitsToCheck2.add(Fruit.GRAPE);
// Checking if the EnumSet contains all elements of the first list
boolean containsAll1 = fruits.containsAll(fruitsToCheck1);
System.out.println("EnumSet contains all elements of the first list: " + containsAll1);
// Checking if the EnumSet contains all elements of the second list
boolean containsAll2 = fruits.containsAll(fruitsToCheck2);
System.out.println("EnumSet contains all elements of the second list: " + containsAll2);
}
}
Output:
EnumSet contains all elements of the first list: true
EnumSet contains all elements of the second list: false
Real-World Use Case
Example: Verifying Selected Days of the Week
A common real-world use case for EnumSet.containsAll()
is verifying if a set of selected days contains all required days for a specific schedule.
Example
import java.util.EnumSet;
import java.util.ArrayList;
import java.util.List;
public class SelectedDaysVerifier {
// 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);
// Creating a list of required days for a specific schedule
List<Day> requiredDays = new ArrayList<>();
requiredDays.add(Day.MONDAY);
requiredDays.add(Day.FRIDAY);
// Checking if the selected days contain all required days
boolean containsAllRequiredDays = selectedDays.containsAll(requiredDays);
// Printing the result
System.out.println("Selected days contain all required days: " + containsAllRequiredDays);
}
}
Output:
Selected days contain all required days: true
In this example, EnumSet.containsAll()
is used to verify if a set of selected days contains all required days for a specific schedule, making it easy to manage and verify schedules.
Conclusion
The EnumSet.containsAll(Collection<?> c)
method in Java provides a way to check if the EnumSet
contains all elements of a specified collection. By understanding how to use this method, you can efficiently manage and verify 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.