Algorithms

EnumSet / Bit Vector

๐Ÿ“‹ Quick Reference

PropertyValue
TypeBit vector for enum constants
Add/Remove/ContainsO(1) - single bit operation
IterationO(n) where n = enum size, not set size
MemoryOne bit per enum constant (extremely compact)
NullNot allowed
Best ForSets of enum values, flags, permissions
One-liner: Ultra-efficient bit vector set for enum types - O(1) operations with minimal memory.

๐ŸŽฎ Interactive Visualizer

Watch how EnumSet uses bits to represent set membership:

Loading visualizer...
Try these operations:
  1. Add elements - see bits flip to 1
  2. Remove elements - see bits flip to 0
  3. Union/intersection - observe bitwise operations
  4. Notice: each enum constant is just one bit!

๐Ÿ”ง Key Operations

Creating

JAVA(17 lines)
Code
Loading syntax highlighter...

Adding/Removing

JAVA(8 lines)
Code
Loading syntax highlighter...

Querying

JAVA(4 lines)
Code
Loading syntax highlighter...

Iteration

JAVA(9 lines)
Code
Loading syntax highlighter...

๐Ÿ“Š Complexity Analysis

OperationTimeNotes
add(e)O(1)Single bit set
remove(e)O(1)Single bit clear
contains(e)O(1)Single bit check
addAll/removeAllO(1)Bitwise OR/AND
size()O(1)Popcount (bit counting)
iterationO(n)n = total enum constants
equalsO(1)Compare bit vectors

Bit Vector Representation

enum Permission { READ, WRITE, EXECUTE, DELETE }

Bit positions:  DELETE  EXECUTE  WRITE  READ
                  3        2       1      0

EnumSet.of(READ, WRITE) = 0011 (binary) = 3
EnumSet.of(READ, EXECUTE) = 0101 (binary) = 5
EnumSet.allOf() = 1111 (binary) = 15
EnumSet.noneOf() = 0000 (binary) = 0

Operations:
add(EXECUTE):    0011 | 0100 = 0111
remove(READ):    0111 & 1110 = 0110
contains(WRITE): 0110 & 0010 = 0010 (truthy)

Internal Implementation

JAVA(5 lines)
Code
Loading syntax highlighter...

โœ… When to Use EnumSet

Good Use Cases

  • Permission/role flags - user permissions, feature flags
  • State combinations - multiple states at once
  • Day/month subsets - working days, quarter months
  • Configuration options - enabled features
  • Replacing bit flags - type-safe alternative to int flags

Avoid When

  • Non-enum types - use HashSet instead
  • Need insertion order - EnumSet uses declaration order
  • Need null elements - EnumSet prohibits null
  • Dynamic values - enums are compile-time fixed

๐Ÿงฉ Common Patterns

Permissions System

JAVA(27 lines)
Code
Loading syntax highlighter...

Feature Flags

JAVA(7 lines)
Code
Loading syntax highlighter...

Replacing Bit Flags

JAVA(9 lines)
Code
Loading syntax highlighter...

โš ๏ธ Common Pitfalls

1. Using HashSet Instead of EnumSet

JAVA(5 lines)
Code
Loading syntax highlighter...

2. Forgetting Declaration Order

JAVA(6 lines)
Code
Loading syntax highlighter...

3. Creating from Empty Collection

JAVA(5 lines)
Code
Loading syntax highlighter...

4. Expecting Null Support

JAVA(6 lines)
Code
Loading syntax highlighter...

๐ŸŽค Interview Tips

Q: Why is EnumSet so fast?
"

It represents the set as a bit vector (typically a single long). Each enum constant maps to a bit position. add/remove/contains are single bit operations - literally O(1) with tiny constant factors.

Q: How much memory does EnumSet use?
"

For enums with โ‰ค64 constants: 8 bytes (one long) plus object overhead. For larger enums: ceil(n/64) longs. Compare to HashSet which uses ~32 bytes per element.

Q: EnumSet vs EnumMap?
"

EnumSet is a Set of enum values (no associated values). EnumMap is a Map with enum keys and any values. Both use array indexing by ordinal for efficiency.

Q: Can EnumSet work with any enum?
"

Yes, any enum type. The type parameter ensures type safety. Implementation automatically chooses RegularEnumSet (โ‰ค64 constants) or JumboEnumSet (>64 constants).


๐Ÿ“š Series Navigation


Visualizer: EnumSet from @tomaszjarosz/react-visualizers