Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Collections

EnumMap and EnumSet

Reference for java.util.EnumMap and EnumSet: compact, fast collections for enum keys, with ordinal ordering, factory methods and range sets.

EnumMap<K extends Enum<K>, V> and EnumSet<E extends Enum<E>> are specialised collections for enum types. Because an enum has a fixed, known set of constants, both classes can store their data in tiny arrays or bit vectors indexed by the constant's ordinal(). They are faster and use less memory than HashMap and HashSet, and they always iterate in the order in which the constants are declared.

Use them whenever the key or element type is an enum: days of the week, states of a workflow, permission flags, card suits.

Java
EnumMap<Day, String> plan = new EnumMap<>(Day.class);
EnumSet<Day> weekend = EnumSet.of(Day.SAT, Day.SUN);

Try it

Java EnumMap keeps declaration order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java EnumSet factories and ranges

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public class EnumMap<K extends Enum<K>, V> extends AbstractMap<K, V>
public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E>
Member Description
new EnumMap<>(Class<K> keyType) Empty map for the given enum type.
new EnumMap<>(Map<K, ? extends V> m) Copy of another map (needs at least one entry unless m is an EnumMap).
EnumSet.noneOf(Class<E>) Empty set.
EnumSet.allOf(Class<E>) Set containing every constant.
EnumSet.of(e1, e2, ...) Set with the listed constants.
EnumSet.range(from, to) Constants from from to to, inclusive, in declaration order.
EnumSet.complementOf(set) Every constant not in set.
EnumSet.copyOf(Collection<E>) Copy of a collection.

Throws: NullPointerException for a null key or element. IllegalArgumentException from range if from comes after to, and from copyOf on an empty non-EnumSet collection.

Usage notes

  • Iteration order is the order of the enum constants, never insertion order. This makes output stable and easy to test.
  • Null: EnumMap rejects null keys but allows null values. EnumSet rejects null elements. Lookups with null (get(null), contains(null)) return null or false instead of throwing.
  • Performance: EnumMap operations are O(1). EnumSet stores membership in one long (up to 64 constants) or a long[], so set operations like addAll and retainAll are very fast.
  • EnumSet has no public constructor. Always use the static factory methods. It is an abstract class with hidden implementations.
  • Not synchronized. Wrap with Collections.synchronizedMap if several threads write.
  • Weakly consistent iterators: they never throw ConcurrentModificationException, but they may or may not show changes made during iteration.
  • Prefer EnumSet over bit flags (int FLAG_A = 1 << 0). It is type safe and just as compact.
  • Ranges depend on declaration order, so reordering constants changes what range returns.