A queue report usually needs a row for every lane, even when the lane is empty. If the code only creates a map entry when an arrival occurs, a lane can disappear from a dashboard or export. An EnumMap is a good fit for this report because its keys are one enum type and its iteration order follows the enum declaration. Prepopulate every lane to make zero counts explicit.

Compile and run

Use JDK 21. Save as QueueReport.java and run the commands below. No build tool or external library is needed. The example throws on a mismatched result without requiring the JVM assertion flag.

import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;

public class QueueReport {
    enum Lane { URGENT, STANDARD, DEFERRED }

    static EnumMap<Lane, Integer> count(List<Lane> arrivals) {
        var counts = new EnumMap<Lane, Integer>(Lane.class);
        for (var lane : Lane.values()) counts.put(lane, 0);
        for (var lane : arrivals) counts.merge(lane, 1, Integer::sum);
        return counts;
    }

    public static void main(String[] args) {
        var report = count(List.of(Lane.STANDARD, Lane.STANDARD, Lane.URGENT));
        if (!new ArrayList<>(report.keySet()).equals(List.of(Lane.values()))
                || report.get(Lane.URGENT) != 1 || report.get(Lane.STANDARD) != 2
                || report.get(Lane.DEFERRED) != 0
                || !count(List.of()).values().stream().allMatch(n -> n == 0)) {
            throw new AssertionError(report);
        }
        report.forEach((lane, n) -> System.out.println(lane + "=" + n));
    }
}
javac QueueReport.java
java QueueReport

Expected output

URGENT=1
STANDARD=2
DEFERRED=0

The arrivals start with STANDARD, but the report starts with URGENT. The map order reflects reporting policy rather than event order. Giving the constructor Lane.class also means it knows the key type when the input is empty. merge combines the previous count with one. It does not sort anything or infer which lanes were supposed to exist.

Adapt the report without changing its contract

If a lane disappears, check that zero initialization happens before counting and that a renderer is not filtering zero rows. Validate external strings before turning them into enum values. Do not use null to mean an unknown lane, since inserting a null key is not supported. For a report that can count beyond the integer range, choose a wider count type and a documented overflow policy. This sample keeps a local mutable map, not a shared live counter.

Declaration order is visible to consumers here. If a report format needs a different order, iterate an explicit list of lanes instead. For durable exchange formats, use stable published codes rather than enum ordinals, which can change when declarations move. If consumers must not mutate the report, enforce that at the return boundary and check that the resulting iteration order still meets the contract.

References and related reading: JDK 21 EnumMap specification and Java Samples Java tutorials.