EnumMap and EnumSet are specialised collections in java.util built for enum keys and enum values. EnumMap is a Map whose keys are the constants of one enum type, and EnumSet is a Set of constants from one enum type. Both are stored in compact arrays or bit vectors, so they are faster and use less memory than HashMap and HashSet for the same job.
Use them whenever your keys or elements are enum constants. This guide shows how each works, the common operations, and the rules to know.
The examples use this enum throughout.
An EnumMap must be created with the enum's Class object, which tells it the key type and the number of constants. Internally it keeps an array indexed by each constant's ordinal().
Iteration follows the natural order of the enum constants (their declaration order), no matter in which order you inserted them. HashMap gives no such guarantee.
null keys are not allowed.null values are permitted.Collections.synchronizedMap if several threads write to it.ConcurrentModificationException.A frequent use is counting or grouping by an enum field. With streams you can ask Collectors.groupingBy to produce an EnumMap.
EnumSet is abstract and has no public constructor. You create one with static factory methods. It is represented as a bit vector, so operations such as contains, add and removeAll are very fast.
| Method | Result |
|---|---|
EnumSet.of(a, b, ...) | Set with the given constants |
EnumSet.noneOf(Day.class) | Empty set of that enum type |
EnumSet.allOf(Day.class) | All constants |
EnumSet.range(from, to) | Constants from from to to, inclusive |
EnumSet.complementOf(set) | Constants not in set |
EnumSet.copyOf(collection) | Copy of another collection of enums |
An EnumSet also rejects null elements and, like EnumMap, iterates in declaration order.
Before EnumSet, programmers stored options as integer bit flags, which is error-prone. An EnumSet gives the same speed with type safety.
| Aspect | EnumMap / EnumSet | HashMap / HashSet |
|---|---|---|
| Key type | One enum type only | Any type |
| Storage | Array / bit vector | Hash table |
| Iteration order | Enum declaration order | Unspecified |
null key or element | Not allowed | Allowed (one) |
| Speed and memory | Better for enums | General purpose |
It needs to know the enum type to size its internal array and to find all constants through reflection on the class. Without it, the map could not tell which constants are valid keys.
No. Synchronise access externally, for example with Collections.synchronizedSet, when several threads modify it.
No. All keys must belong to the same enum type, and a wrong key type fails with a ClassCastException.
No. It keeps the enum's declaration order, not the order in which you added entries.
Use EnumMap when the keys are the constants of one enum, and EnumSet when you need a set of such constants. They are fast, compact and iterate in declaration order. Create an EnumMap with the enum class, create an EnumSet through its factory methods, and remember that neither allows null keys or elements and neither is thread-safe.