java.util · java.base · since Java 1.5
EnumMap
public class EnumMap<K extends Enum<K>, V>
extends AbstractMap<K, V> implements Cloneable, SerializableMap specialized for enum keys: internally an array indexed by ordinal — near-array speed, iteration in declaration order. Always prefer it over HashMap for enum keys (EJ 37).
- Iterates in the enum's declaration (ordinal) order, not insertion order
- Null keys throw NullPointerException — unlike HashMap
Key methods
EnumMap(Class<K> keyType) | The key type token is required at construction. |