Algorithms
EnumSet / Bit Vector
๐ Quick Reference
| Property | Value |
|---|---|
| Type | Bit vector for enum constants |
| Add/Remove/Contains | O(1) - single bit operation |
| Iteration | O(n) where n = enum size, not set size |
| Memory | One bit per enum constant (extremely compact) |
| Null | Not allowed |
| Best For | Sets 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:
- Add elements - see bits flip to 1
- Remove elements - see bits flip to 0
- Union/intersection - observe bitwise operations
- Notice: each enum constant is just one bit!
๐ง Key Operations
Creating
JAVA(17 lines)CodeLoading syntax highlighter...
Adding/Removing
JAVA(8 lines)CodeLoading syntax highlighter...
Querying
JAVA(4 lines)CodeLoading syntax highlighter...
Iteration
JAVA(9 lines)CodeLoading syntax highlighter...
๐ Complexity Analysis
| Operation | Time | Notes |
|---|---|---|
add(e) | O(1) | Single bit set |
remove(e) | O(1) | Single bit clear |
contains(e) | O(1) | Single bit check |
addAll/removeAll | O(1) | Bitwise OR/AND |
size() | O(1) | Popcount (bit counting) |
iteration | O(n) | n = total enum constants |
equals | O(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)CodeLoading 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)CodeLoading syntax highlighter...
Feature Flags
JAVA(7 lines)CodeLoading syntax highlighter...
Replacing Bit Flags
JAVA(9 lines)CodeLoading syntax highlighter...
โ ๏ธ Common Pitfalls
1. Using HashSet Instead of EnumSet
JAVA(5 lines)CodeLoading syntax highlighter...
2. Forgetting Declaration Order
JAVA(6 lines)CodeLoading syntax highlighter...
3. Creating from Empty Collection
JAVA(5 lines)CodeLoading syntax highlighter...
4. Expecting Null Support
JAVA(6 lines)CodeLoading 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
Previous: Part 7: LinkedHashMap
Next: Part 9: Binary Search
Visualizer:
EnumSet from @tomaszjarosz/react-visualizers