EnumSet
About
EnumSet
is a specialized Set
implementation for use with enum types. It is a high-performance, memory-efficient collection that stores elements of an enumerated type in a bit-vector, enabling operations like iteration and manipulation to be faster compared to other Set
implementations. All elements in an EnumSet
must belong to the same enum type.
Features
Enum-Specific: Works exclusively with enums.
High Performance: Internally implemented as a bit vector, making operations faster.
Compact Representation: Uses less memory compared to other
Set
implementations likeHashSet
.Natural Order: Maintains elements in their natural order as defined in the enum declaration.
Efficient Range Operations: Provides methods to create sets based on ranges of enum constants.
No Nulls: Does not allow
null
values.Not Thread-Safe: Requires external synchronization for concurrent access.
Internal Working
Bit-Vector Representation
The
EnumSet
uses a bit-vector representation internally.Each bit in the vector represents a specific constant of the enum type.
For example, if an enum has three constants,
A
,B
, andC
, the bit positions representA=1
,B=2
, andC=4
.
Storage Efficiency
For enums with fewer constants, it uses a single
long
to represent the set.For enums with a larger number of constants, it switches to an array of
long
values.
Underlying Implementations
RegularEnumSet: Used for enums with fewer constants (fits in a single
long
).JumboEnumSet: Used for enums with a large number of constants (requires multiple
long
values).
Operations
Adding an element sets the corresponding bit in the vector.
Removing an element clears the corresponding bit.
Checking for an element involves inspecting the bit corresponding to the constant.
Key Methods
Method
Description
of(E... elements)
Creates an EnumSet
containing the specified elements.
allOf(Class<E> elementType)
Creates an EnumSet
containing all elements of the specified enum type.
noneOf(Class<E> elementType)
Creates an empty EnumSet
of the specified enum type.
range(E from, E to)
Creates an EnumSet
containing elements within the specified range.
add(E e)
Adds the specified element to the set.
addAll(Collection<E>)
Adds all elements from the specified collection.
remove(E e)
Removes the specified element from the set.
contains(E e)
Returns true
if the set contains the specified element.
size()
Returns the number of elements in the set.
iterator()
Returns an iterator for the set elements.
clone()
Creates a shallow copy of the EnumSet
.
Big-O for Operations
Operation
Time Complexity
Add
O(1)
Remove
O(1)
Contains
O(1)
Iteration
O(n)
Limitations
Enum-Only: Works exclusively with enum types and cannot be used for general objects.
Non-Thread-Safe: Requires external synchronization for concurrent use.
No Null Values: Does not support
null
elements.
Real-World Usage
State Tracking: Tracking states or flags represented by enums.
Permission Management: Managing access levels or permissions using enums.
Efficient Range Queries: Selecting subsets of enum constants for processing.
Configuration: Representing configurations where options are predefined.
Examples
1. Basic Operations
2. Creating from Range
3. Creating Empty and All Elements Set
4. Using Clone Method
Last updated
Was this helpful?