The EnumSet
class in Java is a specialized Set implementation designed for use with enum types. It is a part of the Java Collections Framework and provides a high-performance set implementation specifically for enums. EnumSet
is much faster than other Set implementations, making it an excellent choice for use with enum constants.
This guide covers various methods available in the EnumSet
class, offering a comprehensive understanding of how to manipulate and interact with enum sets in Java. These methods are crucial for efficient coding practices and help in performing operations like adding, removing, and checking for elements.
For more detailed information, you can refer to the official Java SE Documentation and additional resources on Java Collections Tutorial.
EnumSet Class Methods
Method | Description |
---|---|
allOf() | Creates an EnumSet containing all of the elements in the specified enum type. |
noneOf() | Creates an empty EnumSet with the specified enum type. |
of() | Creates an EnumSet initially containing the specified elements. |
complementOf() | Creates an EnumSet containing all of the elements in the specified collection that are not in this EnumSet. |
copyOf() | Creates an EnumSet with the same elements as the specified EnumSet. |
add() | Adds the specified element to the EnumSet. |
addAll() | Adds all of the elements in the specified collection to the EnumSet. |
contains() | Checks if the EnumSet contains the specified element. |
containsAll() | Checks if the EnumSet contains all of the elements in the specified collection. |
remove() | Removes the specified element from the EnumSet. |
removeAll() | Removes from the EnumSet all of its elements that are contained in the specified collection. |
retainAll() | Keeps only the elements in the EnumSet that are contained in the specified collection. |
References: