There are many good answers here, but none mentiones that there are highly optimized implementations of the Collection API classes/interfaces specifically for enums:
These enum specific classes only accept Enum
instances (the EnumMap
only accept Enum
s only as keys), and whenever possible, they revert to compact representation and bit manipulation in their implementation.
What does this mean?
If our Enum
type has no more that 64 elements (most of real-life Enum
examples will qualify for this), the implementations store the elements in a single long
value, each Enum
instance in question will be associated with a bit of this 64-bit long long
. Adding an element to an EnumSet
is simply just setting the proper bit to 1, removing it is just setting that bit to 0. Testing if an element is in the Set
is just one bitmask test! Now you gotta love Enum
s for this!