What is EnumMap in Java

What is EnumMap in Java

EnumMap is a specialized implementation of the Map data structure in which the keys are enums. It’s particularly useful in scenarios where we want to retrieve a value corresponding to an enum constant. In this article, we’ll explore how to work with EnumMap and provide examples to aid understanding.

Let’s assume we want to implement a mapping function that accepts a Java DayOfWeek enum as input and returns the corresponding day name in German. One way to implement this is as follows:

private static final Map<String, String> germanDayOfWeeks = new HashMap<>();

static {
  germanDayOfWeeks.put(DayOfWeek.MONDAY.name(), "Montag");
  germanDayOfWeeks.put(DayOfWeek.TUESDAY.name(), "Dienstag");
  germanDayOfWeeks.put(DayOfWeek.WEDNESDAY.name(), "Mittwoch");
  germanDayOfWeeks.put(DayOfWeek.THURSDAY.name(), "Donnerstag");
  germanDayOfWeeks.put(DayOfWeek.FRIDAY.name(), "Freitag");
  germanDayOfWeeks.put(DayOfWeek.SATURDAY.name(), "Samstag");
  germanDayOfWeeks.put(DayOfWeek.SUNDAY.name(), "Sonntag");
}

public static String getCorrespondingGermanDayOfWeek(DayOfWeek englishDayOfWeek) {
  return germanDayOfWeeks.get(englishDayOfWeek.name());
}

In the example above, we use a HashMap to store DayOfWeek.name() (which returns a String) as the key, and the corresponding German day name as the value. To make it easier for the client code, we accept a DayOfWeek parameter in the getCorrespondingGermanDayOfWeek method and call .name() within the method to obtain its string representation.

This approach works perfectly. We can even write a quick test for it:

@ParameterizedTest
@CsvSource(value = {"MONDAY,Montag", "TUESDAY,Dienstag", "WEDNESDAY,Mittwoch",
    "THURSDAY,Donnerstag", "FRIDAY,Freitag", "SATURDAY,Samstag", "SUNDAY,Sonntag"})
void shouldRetrieveCorrespondingDayOfWeekInGerman(DayOfWeek dayOfWeek, String expectedGermanDayOfWeek) {
  var germanDayOfWeek = EnumMapExample.getCorrespondingGermanDayOfWeek(dayOfWeek);
  assertEquals(expectedGermanDayOfWeek, germanDayOfWeek);
}

However, if we take a closer look at the code, we’ll notice that we repeatedly call the .name() method wherever we use DayOfWeek. Wouldn’t it be simpler if we could eliminate that step?

As mentioned earlier, EnumMap is an implementation of the Map interface designed specifically for use with enum keys. Let’s refactor the code using EnumMap:

private static final EnumMap<DayOfWeek, String> germanDayOfWeeks = new EnumMap<>(DayOfWeek.class);

static {
  germanDayOfWeeks.put(DayOfWeek.MONDAY, "Montag");
  germanDayOfWeeks.put(DayOfWeek.TUESDAY, "Dienstag");
  germanDayOfWeeks.put(DayOfWeek.WEDNESDAY, "Mittwoch");
  germanDayOfWeeks.put(DayOfWeek.THURSDAY, "Donnerstag");
  germanDayOfWeeks.put(DayOfWeek.FRIDAY, "Freitag");
  germanDayOfWeeks.put(DayOfWeek.SATURDAY, "Samstag");
  germanDayOfWeeks.put(DayOfWeek.SUNDAY, "Sonntag");
}

public static String getCorrespondingGermanDayOfWeek(DayOfWeek englishDayOfWeek) {
  return germanDayOfWeeks.get(englishDayOfWeek);
}

As you can see, the refactored code is cleaner and avoids unnecessary calls to .name() on each enum constant.

Since EnumMap is a subtype of Map, all the usual map functionalities—such as put(), get(), etc.—are supported.

Conclusion

In this article, we explored what EnumMap is and demonstrated how it can simplify and clean up your code when working with enums. As always, the tutorial’s source code is available on the Geeky Hacker GitHub repository.