Understanding Java EnumMap: Concept, Usage, and Implementation
This article explains Java's EnumMap, covering its definition, key characteristics, how to create and use it with code examples, its efficient array‑based implementation, and practical test cases, demonstrating why EnumMap is a high‑performance map for enum keys in backend development.
In Java, the enum type is a useful data type for defining a fixed set of constants, and EnumMap is a high‑performance Map implementation based on enum types.
EnumMap stores key‑value pairs in an internal array, ensuring all enum values are covered and providing fast lookup, insertion, and removal.
What is EnumMap
EnumMap is a generic class extending AbstractMap and implementing Serializable and Cloneable . Its key type must be an enum, while the value type can be any object.
public class EnumMap
, V> extends AbstractMap
implements java.io.Serializable, CloneableHow to use EnumMap
Define an enum, then create an EnumMap instance with the enum class, and put entries using put() , retrieve with get() , and remove with remove() .
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EnumMapTest {
enum Color { RED, GREEN, BLUE }
@Test
public void testColorEnumMap() {
EnumMap
colorMap = new EnumMap<>(Color.class);
colorMap.put(Color.RED, "#FF0000");
colorMap.put(Color.GREEN, "#00FF00");
colorMap.put(Color.BLUE, "#0000FF");
System.out.println(colorMap);
}
}The output shows the mapping of each enum constant to its corresponding string.
Implementation principle
EnumMap uses an internal Object array whose length equals the number of enum constants; each position stores the value for the corresponding enum key, enabling O(1) access.
Advanced example – Command pattern
interface Command { void execute(); }
class StartCommand implements Command { @Override public void execute() { System.out.println("Start command executed"); } }
class StopCommand implements Command { @Override public void execute() { System.out.println("Stop command executed"); } }
enum CommandType { START, STOP }
public class CommandExample {
private static final EnumMap
commandMap = new EnumMap<>(CommandType.class);
static {
commandMap.put(CommandType.START, new StartCommand());
commandMap.put(CommandType.STOP, new StopCommand());
}
public static void main(String[] args) {
commandMap.get(CommandType.START).execute();
commandMap.get(CommandType.STOP).execute();
}
}This demonstrates using EnumMap to map enum command types to concrete command implementations.
Test case
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EnumMapTest {
enum Weekday { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
@Test
public void testWeekDayEnumMap() {
EnumMap
map = new EnumMap<>(Weekday.class);
map.put(Weekday.MONDAY, "星期一");
map.put(Weekday.TUESDAY, "星期二");
map.put(Weekday.WEDNESDAY, "星期三");
System.out.println(" map = " + map);
assertTrue(map.containsKey(Weekday.MONDAY));
assertFalse(map.containsKey(Weekday.SATURDAY));
System.out.println(" map.size() = " + map.size());
assertEquals(3, map.size());
System.out.println("map.get(Weekday.MONDAY) = " + map.get(Weekday.MONDAY));
assertEquals("星期一", map.get(Weekday.MONDAY));
map.remove(Weekday.MONDAY);
assertEquals(2, map.size());
}
}The test verifies basic EnumMap operations such as insertion, lookup, size checking, and removal.
Conclusion
EnumMap provides a convenient and highly efficient way to map enum keys to values, making it especially suitable for scenarios where enums are used as map keys in backend development.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.