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.
EnumMap<Day, String> plan = new EnumMap<>(Day.class); EnumSet<Day> weekend = EnumSet.of(Day.SAT, Day.SUN);
Try it
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.
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
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:
EnumMaprejectsnullkeys but allowsnullvalues.EnumSetrejectsnullelements. Lookups withnull(get(null),contains(null)) returnnullorfalseinstead of throwing. - Performance:
EnumMapoperations areO(1).EnumSetstores membership in onelong(up to 64 constants) or along[], so set operations likeaddAllandretainAllare very fast. EnumSethas no public constructor. Always use the static factory methods. It is an abstract class with hidden implementations.- Not synchronized. Wrap with
Collections.synchronizedMapif several threads write. - Weakly consistent iterators: they never throw
ConcurrentModificationException, but they may or may not show changes made during iteration. - Prefer
EnumSetover 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
rangereturns.