C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Put: We add elements into the EnumMap with put. The new key is the first argument. The new value is the second.
Get: This fetches a value from the map based on a key. Here we use constant enum values, but variables also work.
Java program that uses EnumMap
import java.util.EnumMap;
public class Program {
enum Importance {
Low, Medium, High, Critical
}
public static void main(String[] args) {
// Create an EnumMap.
EnumMap<Importance, String> e = new EnumMap<>(Importance.class);
e.put(Importance.Low, "=Low");
e.put(Importance.High, "=High");
// Get values from the map.
String value1 = e.get(Importance.Low);
String value2 = e.get(Importance.High);
System.out.println(value1);
System.out.println(value2);
}
}
Output
=Low
=High
Java program that uses containsKey
import java.util.EnumMap;
public class Program {
enum Importance {
Low, Medium, High, Critical
}
public static void main(String[] args) {
EnumMap<Importance, String> e = new EnumMap<>(Importance.class);
e.put(Importance.Critical, "=Critical");
e.put(Importance.High, "=High");
// This is not present.
System.out.println(e.containsKey(Importance.Low));
// This is present in the EnumMap.
if (e.containsKey(Importance.Critical)) {
System.out.println(1);
}
}
}
Output
false
1
Java program that uses keySet
import java.util.EnumMap;
import java.util.Set;
public class Program {
enum Size {
Small, Normal, Large
}
public static void main(String[] args) {
EnumMap<Size, Integer> e = new EnumMap<>(Size.class);
e.put(Size.Small, 10);
e.put(Size.Normal, 20);
// Get keys and loop over them.
Set<Size> set = e.keySet();
for (Size s : set) {
System.out.println(s);
}
}
}
Output
Small
Normal
Version 1: This version of the code uses EnumMap to look up an Enum defined in the program.
Version 2: Here we do the same thing as version 1 but we use a HashMap. We call containsKey on an enum.
Result: A program that uses enum keys in a HashMap (or other collection) will benefit from EnumMap. The benefits are clear.
Java program that tests EnumMap performance
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);
}
}
Output
85 ms, EnumMap lookup
144 ms, HashMap lookup