Java EnumMap with Examples

Learn to create and work with the Java EnumMap in detail, how it differs from the HashMap, and the practical use-cases for the same.

1. Introduction to Java EnumMap

EnumMap is a specialized Map implementation that uses keys as enum type. When the map is created, all of the map’s keys must come from a single enum type, explicitly or implicitly.

enum Direction {

EnumMap<Direction, Boolean> allowedDirectionsMap = new EnumMap<>(Direction.class);
allowedDirectionsMap.put(Direction.EAST, true);

The EnumMap class (present in java.util package) extends AbstractMap class and has been present since Java version 1.5.

  • This class uses enum type as its key and all the keys of an EnumMap instance must be of a single enum type.
  • All elements in EnumMap are ordered in the natural order of map keys (the order in which enum constants are declared inside enum type).
  • It is not a thread-safe class. Use Collections.synchronizedMap() to get a thread-safe reference of this class.
  • Its iterators do not throw ConcurrentModificationException while attempting to modify the map during the iteration.

In Java Collections, the class has been declared as follows:

public class EnumMap<K extends Enum<K>, V> extends AbstractMap<K, V> implements Serializable, Cloneable
Java EnumMap

2. Working with EnumMap

2.1. Creating EnumMap

We can create EnumMap by making use of the following constructors:

  • EnumMap(Class keyType): Used to create an empty map with the specified keyType.
  • EnumMap(EnumMap m): Used to create a map with the same key type as the specified Enum Map.
  • EnumMap(Map m): Used to create an Enum Map initialized from the specified map.
EnumMap<Direction, Boolean> allowedDirectionsMap = new EnumMap<>(Direction.class);

EnumMap<Direction, Boolean> newMap = new EnumMap<>(allowedDirectionsMap);

EnumMap<Direction, Boolean> newMapFromAnotherMap = new EnumMap<>(Map.of(EAST, true));

2.2. EnumMap Methods

EnumMap supports all standard methods available in the Map interface.

  • Object put(key, value): inserts a key-value pair into the map.
  • Object get(key): returns the value for the specified key in the map.
  • boolean containsKey(key): returns true or false based on whether the specified key is found in the map or not.
  • boolean containsValue(value): similar to containsKey() method, it looks for the specified value instead of key.
  • Set keySet(): returns the Set of all keys stored in the map.
  • Set entrySet(): returns the Set of all mappings stored in the map.
  • Value remove(Object key): removes the key-value pair for the specified key.
  • int size(): returns the map size equal to the number of key-value pairs stored in the map.
public enum Planet

// Creating EnumMap
EnumMap<Planet, String> map = new EnumMap<>(Planet.class);

// Putting elements in map
map.put(Planet.MERCURY, “Closest Planet to Sun”);
map.put(Planet.JUPITER, “Largest Planet”);
map.put(Planet.VENUS, “Hottest Planet”);

System.out.println(map);       // {MERCURY=Closest Planet to Sun, VENUS=Hottest Planet, JUPITER=Largest Planet}

// Getting a value from the map
System.out.println(map.get(Planet.VENUS));     // Hottest Planet

// Checking if a key or value present in the map
System.out.println(map.containsKey(Planet.JUPITER));       // true
System.out.println(map.containsValue(“Red Planet”));       // false

// Removing an entry

// Finding map size
System.out.println(map.size());      // 2

// Iterating over the map
for(Map.Entry<Planet, String> entry : map.entrySet())
    System.out.println(entry.getKey() + " :: " + entry.getValue());

3. Difference Between Java EnumMap and HashMap

3.1 Creation

  • In EnumMap, only enum type keys are allowed. Also, we need to specify the key type in the constructor while creating the map.
  • In HashMap, all types of keys are allowed, including enums.
// EnumMap Creation
EnumMap<Planet, String> enumMap = new EnumMap<>(Planet.class);

// HashMap Creation
HashMap<String, String> hashMap = new HashMap<>(); 

3.2. NULL Keys

  • EnumMap doesn’t allow null keys but supports any number of null values. It throws NullPointerException in runtime.
  • HashMap supports one null key and any number of null values.
EnumMap<Planet, String> map = new EnumMap<>(Planet.class);
map.put(null, "value1");   //NullPointerException in runtime

3.3. Internal Implementations

EnumMap uses Arrays internally, whereas HashMap uses HashTable and is based on the technique of Hashing.

3.4. Element Ordering

  • Elements in the EnumMap are ordered on the natural order of the keys, in the order in which the keys/constants are declared in the enum type.
  • HashMap doesn’t maintain any ordering for its keys, so we cannot guarantee that the element inserted first in the map will be retrieved first during the iteration.

3.5. Performance

EnumMap performs better as compared with HashMap due to the following reasons:

  • EnumMap is internally backed by an array and uses ordinal() method while inserting the elements in the map using the put() method, which eliminates the chances of hash collisions which is very common in the case of HashMap, which uses hashCode() method for inserting the elements.
  • In EnumMap, the keys are known in advance as it uses enum constants as its key. Due to this, it benefits from additional performance optimization compared with HashMap.

4. Conclusion

We learned about Enum Map in Java, its constructors, essential methods, and how it differs from HashMap. We have also covered the practical examples for the same and how we can create it from other maps.

Happy Learning !!

Sourcecode on Github

Leave a Reply

Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial