Maps: EnumMap is the right map for enum keys

EnumMap is backed by an array indexed by ordinal, so it is compact, fast, and always iterates in enum declaration order.

Code
enum Status { NEW, ACTIVE, CLOSED }

var counts = new EnumMap<Status, Integer>(Status.class);
counts.put(Status.CLOSED, 5);
counts.put(Status.NEW, 2);
counts.put(Status.ACTIVE, 7);

System.out.println("iterates in declaration order: " + counts);
System.out.println("ACTIVE = " + counts.get(Status.ACTIVE));
Output
iterates in declaration order: {NEW=2, ACTIVE=7, CLOSED=5}
ACTIVE = 7
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-07-30