EnumMap and EnumSet — Performance Advantages
Fast recall
Quick revision
Core answer
Awareness of enum-specialized collections and their O(1) bitwise implementations
Using HashMap<MyEnum, V> when EnumMap would be more efficient, or not knowing that EnumSet is backed by a long bitmask
Explain that EnumSet operations (add, contains, removeAll with another EnumSet) are implemented as bitwise AND/OR/XOR on a single long (for enums up to 64 constants), making them extremely fast. Note that EnumMap maintains iteration order matching enum declaration order.
Interview-ready guide
Interview answer
EnumMap and EnumSet exploit the small, fixed set of enum values to deliver performance that no general-purpose collection can match.
EnumMap:
- Flat array indexed by
ordinal()—put()/get()are direct array access with no hashing, no collisions, no Node objects - Fraction of HashMap's memory; deterministic iteration in enum declaration order
EnumSet:
- For enums with ≤ 64 constants: membership stored in a single
long— 8 bytes total add()sets a bit;contains()is a bitwise AND;retainAll()between two EnumSets is a single CPU AND instruction- For > 64 constants: uses a
long[]array (JumboEnumSet)
When to use:
- Enum keys → EnumMap; enum elements → EnumSet
- No trade-off — they are faster, smaller, and more predictable in every dimension vs HashMap/HashSet
I recommend EnumMap/EnumSet as the obvious default whenever your keys or elements are enums.
EnumMap and EnumSet — Performance Advantages
Direct Answer
EnumSet is backed by a long bitmask — set operations are bitwise AND/OR/XOR, making them faster than any hash-based set. EnumMap uses a plain array indexed by enum ordinal — no hashing, no collisions, O(1) with zero overhead. Both require that keys/elements are enum constants.
Deep Dive & Architecture
compute, merge, computeIfAbsent, and computeIfPresent are Map methods (Java 8+) that atomically combine get + check + put into a single operation, replacing the verbose get-check-put anti-pattern. computeIfAbsent(key, mappingFunction) — if key absent, compute value and put; most-used for lazy initialization of multi-valued maps: map.computeIfAbsent(key, k -> new ArrayList<>()).add(value). computeIfPresent(key, remappingFunction) — if key present, recompute value (or remove if null returned). compute(key, remappingFunction) — recompute regardless of presence (function gets old value or null). merge(key, value, remappingFunction) — if absent, put value; if present, apply function to combine: map.merge(word, 1, Integer::sum) is the idiomatic word-counter. Why they matter: (1) atomic in ConcurrentHashMap — no lost updates from concurrent get-check-put races; (2) less code than if (map.containsKey(k)) map.put(k, ...) else map.put(k, ...); (3) cleaner intent — the operation says what it does. Pitfalls: (1) not atomic in non-concurrent maps (HashMap) — only the API is single-call, not thread-safe; (2) returning null from a remapping function removes the key — surprising; (3) don't recursively modify the map inside the function — undefined behavior; (4) merge doesn't handle null values the same way as compute — read the Javadoc. For interviews: lead with 'replaces get-check-put, atomic in ConcurrentHashMap, idiomatic for multimap lazy-init and counters'.
EnumSet is backed by a long bitmask — set operations are bitwise AND/OR/XOR, making them faster than any hash-based set. EnumMap uses a plain array indexed by enum ordinal — no hashing, no collisions, O(1) with zero overhead. Both require that keys/elements are enum constants.
A common mistake is confusing the interface with the implementation (e.g., saying List when you mean ArrayList), or not knowing that Map does not extend Collection.
Common Mistakes & Pitfalls
Using HashMap<MyEnum, V> when EnumMap would be more efficient, or not knowing that EnumSet is backed by a long bitmask
Check your understanding
Answer these without looking back at the article.
- What is the Java Collections Framework (JCF) and why does it exist? It provides a unified architecture of interfaces and implementations for storing and manipulating groups of objects, replacing the ad-hoc pre-JCF classes (Vector, Hashtable, Enumeration) with a consistent, interoperable API.
- How does HashSet work internally in Java?
- How does HashMap internally handle hash collisions in Java 8+? Explain the transition from linked list to tree.
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.