The EnumMap is faster for lookups than a HashMap with enum keys. In this benchmark, I found EnumMap is a bit less than twice as fast on simple lookups.
import java.util.HashMap;
import java.util.EnumMap;
public class Program {
enum Importance {
Low, Medium, High, Critical
}
public static void main(String[] args) throws Exception {
EnumMap<Importance, String> e = new EnumMap<>(Importance.class);
e.put(Importance.Low,
"=Low");
e.put(Importance.High,
"=High");
HashMap<Importance, String> h = new HashMap<>();
h.put(Importance.Low,
"=Low");
h.put(Importance.High,
"=High");
long t1 = System.currentTimeMillis();
// Version 1: check EnumMap.
for (int i = 0; i < 10000000; i++) {
if (!e.containsKey(Importance.Low)) {
throw new Exception();
}
}
long t2 = System.currentTimeMillis();
// Version 2: check HashMap.
for (int i = 0; i < 10000000; i++) {
if (!h.containsKey(Importance.Low)) {
throw new Exception();
}
}
long t3 = System.currentTimeMillis();
// ... Times.
System.out.println(t2 - t1);
System.out.println(t3 - t2);
}
}
85 ms, EnumMap lookup
144 ms, HashMap lookup