EnumMap is a specialized implementation of the Map interface designed specifically for enum keys. It stores key-value pairs where all keys belong to a single enum type. EnumMap is part of the java.util package and provides better performance and lower memory consumption than HashMap when working with enum keys.
- Not synchronized.
- Internally implemented using arrays for efficient storage.
- All keys must belong to the same enum type.
import java.util.EnumMap;
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public class Geeks {
public static void main(String[] args) {
// Create an EnumMap for Day enum
EnumMap<Day, String> dayMap = new EnumMap<>(Day.class);
// Add elements to the EnumMap
dayMap.put(Day.MONDAY, "Start of the week");
dayMap.put(Day.FRIDAY, "End of the week");
dayMap.put(Day.SUNDAY, "Weekend");
// Print all elements in the EnumMap
for (Day day : dayMap.keySet()) {
System.out.println(day + ": " + dayMap.get(day));
}
}
}
Output
MONDAY: Start of the week FRIDAY: End of the week SUNDAY: Weekend
Explanation: In this example, an EnumMap is created using the Day enum as the key type. Three key-value pairs are added using the put() method, and the entries are printed in the natural order of the enum constants.
EnumMap Hierarchy
EnumMap extends the AbstractMap class and implements the Map interface, providing a specialized map implementation for enum keys with high performance and memory efficiency.

Declaration
An EnumMap is created by specifying the enum class whose constants will be used as keys.
EnumMap<EnumType, ValueType> map = new EnumMap<>(EnumType.class);
Where
- EnumType represents the enum used as keys.
- ValueType represents the type of values stored in the map.
Example: Storing and Retrieving Values
import java.util.EnumMap;
enum Days {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
}
public class Geeks {
public static void main(String[] args)
{
EnumMap<Days, String> e = new EnumMap<>(Days.class);
// Adding elements to the EnumMap
e.put(Days.MONDAY, "Work");
e.put(Days.TUESDAY, "Work");
e.put(Days.WEDNESDAY, "Study");
e.put(Days.THURSDAY, "Study");
e.put(Days.FRIDAY, "Relax");
// Getting elements from the EnumMap
System.out.println(e.get(Days.MONDAY));
System.out.println(e.get(Days.FRIDAY));
}
}
Output
Work Relax
Explanation: In this example, we define an enum type Days that represents the days of the week. We then create an EnumMap and add elements to it using the put method. Finally, we retrieve elements from the EnumMap using the get method and print the results to the console.
Constructors of EnumMap
1. EnumMap(Class keyType)
Creates an empty EnumMap with the specified enum type as its keys.
Syntax:
EnumMap<EnumType, ValueType> map = new EnumMap<>(EnumType.class);
2. EnumMap(EnumMap m)
Creates a new EnumMap by copying all mappings from the specified EnumMap.
Syntax:
EnumMap<EnumType, ValueType> map2 = new EnumMap<>(map1);
3. EnumMap(Map m)
Creates an EnumMap containing all mappings from the specified map, provided the map uses a single enum type as keys.
Syntax:
EnumMap<EnumType, ValueType> map = new EnumMap<>(existingMap);
Example: Program to create, perform basic operation
import java.util.EnumMap;
public class Geeks {
// Defining an enum called GFG
public enum GFG {
CODE,
CONTRIBUTE,
QUIZ,
MCQ
}
public static void main(String[] args) {
// Creating an EnumMap where the key is
// of type GFG and the value is a String
EnumMap<GFG, String> e = new EnumMap<>(GFG.class);
// Adding key-value pairs to the map
e.put(GFG.CODE, "Start Coding with GFG");
e.put(GFG.CONTRIBUTE, "Contribute for others");
e.put(GFG.QUIZ, "Practice Quizzes");
e.put(GFG.MCQ, "Test Speed with MCQs");
// Printing the size of the EnumMap
System.out.println("Size of EnumMap: " + e.size());
// Printing the contents of the EnumMap
// The map will print in the natural order of
// enum keys (the order in which they are defined)
System.out.println("EnumMap: " + e);
// Retrieving a value from the EnumMap using a specific key
System.out.println("Value for CODE: " + e.get(GFG.CODE));
// Checking if the EnumMap contains a specific key
System.out.println("Does gfgMap contain CONTRIBUTE? "
+ e.containsKey(GFG.CONTRIBUTE));
// Checking if the EnumMap contains a specific value
System.out.println("Does gfgMap contain the value 'Practice Quizzes'? "
+ e.containsValue("Practice Quizzes"));
// Checking if the EnumMap contains a null value
// (which is not present in this example)
System.out.println("Does gfgMap contain a null value? "
+ e.containsValue(null));
}
}
Output:

Explanation: In this example, an EnumMap stores activities for different days of the week. The put() method inserts mappings, and the get() method retrieves values associated with specific enum keys.
Performing Various Operations on LinkedTransferQueue
1. Adding Elements: We can use put() and putAll() method to insert elements to a EnumMap.
import java.util.EnumMap;
class Geeks {
enum Color { RED, GREEN, BLUE, WHITE }
public static void main(String[] args)
{
// Creating an EnumMap of the Color enum
EnumMap<Color, Integer> e
= new EnumMap<>(Color.class);
// Insert elements in Map
// using put() method
e.put(Color.RED, 1);
e.put(Color.GREEN, 2);
// Printing mappings to the console
System.out.println("EnumMap colors1: " + e);
// Creating an EnumMap of the Color Enum
EnumMap<Color, Integer> e1
= new EnumMap<>(Color.class);
// Adding elements using the putAll() method
e1.putAll(e);
e1.put(Color.BLUE, 3);
// Printing mappings to the console
System.out.println("EnumMap colors2: " + e1);
}
}
Output
EnumMap colors1: {RED=1, GREEN=2}
EnumMap colors2: {RED=1, GREEN=2, BLUE=3}
2. Accessing Elements: We can use entrySet(), keySet(), values() and get() to access the elements of EnumMap.
import java.util.EnumMap;
class Geeks {
// Enum
enum Color { RED, GREEN, BLUE, WHITE }
public static void main(String[] args)
{
// Creating an EnumMap of the Color enum
EnumMap<Color, Integer> e
= new EnumMap<>(Color.class);
// Inserting elements using put() method
e.put(Color.RED, 1);
e.put(Color.GREEN, 2);
e.put(Color.BLUE, 3);
e.put(Color.WHITE, 4);
System.out.println("EnumMap: " + e);
// Using the entrySet() method
System.out.println("Key-Value mappings: "
+ e.entrySet());
// Using the keySet() method
System.out.println("Keys: " + e.keySet());
// Using the values() method
System.out.println("Values: " + e.values());
// Using the get() method
System.out.println("Value of RED : "
+ e.get(Color.RED));
}
}
Output
EnumMap: {RED=1, GREEN=2, BLUE=3, WHITE=4}
Key-Value mappings: [RED=1, GREEN=2, BLUE=3, WHITE=4]
Keys: [RED, GREEN, BLUE, WHITE]
Values: [1, 2, 3, 4]
Value of RED : 1
3. Removing Elements: We can use the remove() method to remove elements from the EnumMap.
import java.util.EnumMap;
class Geeks {
enum Color {
// Custom elements
RED,
GREEN,
BLUE,
WHITE
}
public static void main(String[] args)
{
// Creating an EnumMap of the Color enum
EnumMap<Color, Integer> e
= new EnumMap<>(Color.class);
// Inserting elements in the Map
// using put() method
e.put(Color.RED, 1);
e.put(Color.GREEN, 2);
e.put(Color.BLUE, 3);
e.put(Color.WHITE, 4);
// Printing e in the EnumMap
System.out.println("EnumMap e : " + e);
// Removing a mapping
// using remove() Method
int i = e.remove(Color.WHITE);
// Displaying the removed value
System.out.println("Removed Value: " + i);
// Removing specific color and storing boolean
// if removed or not
boolean b = e.remove(Color.RED, 1);
// Printing the boolean result whether removed or
// not
System.out.println("Is the entry {RED=1} removed? "
+ b);
// Printing the updated Map to the console
System.out.println("Updated EnumMap: " + e);
}
}
Output
EnumMap e : {RED=1, GREEN=2, BLUE=3, WHITE=4}
Removed Value: 4
Is the entry {RED=1} removed? true
Updated EnumMap: {GREEN=2, BLUE=3}
4. Replacing Elements: Map interface provides three variations of the replace() method to change the mappings of EnumMap.
import java.util.EnumMap;
class Geeks {
enum Color {
RED,
GREEN,
BLUE,
WHITE
}
public static void main(String[] args)
{
// Creating an EnumMap of the Color enum
EnumMap<Color, Integer> e
= new EnumMap<>(Color.class);
// Inserting elements to Map
// using put() method
e.put(Color.RED, 1);
e.put(Color.GREEN, 2);
e.put(Color.BLUE, 3);
e.put(Color.WHITE, 4);
// Printing all elements inside above Map
System.out.println("EnumMap e " + e);
// Replacing certain elements depicting e
// using the replace() method
e.replace(Color.RED, 11);
e.replace(Color.GREEN, 2, 12);
// Printing the updated elements (e)
System.out.println("EnumMap using replace(): "
+ e);
// Replacing all e using the replaceAll()
// method
e.replaceAll((key, oldValue) -> oldValue + 3);
// Printing the elements of above Map
System.out.println("EnumMap using replaceAll(): "
+ e);
}
}
Output
EnumMap e {RED=1, GREEN=2, BLUE=3, WHITE=4}
EnumMap using replace(): {RED=11, GREEN=12, BLUE=3, WHITE=4}
EnumMap using replaceAll(): {RED=14, GREEN=15, BLUE=6, WHITE=7}
Synchronized Enum Map
The implementation of an EnumMap is not synchronized. This means that if multiple threads access a tree set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by using the synchronizedMap() method of the Collections class. This is best done at the creation time, to prevent accidental unsynchronized access.
Map<EnumKey, V> m = Collections.synchronizedMap(new EnumMap<EnumKey, V>(...));
Advantages of EnumMap
- Faster than HashMap when enum keys are used.
- Uses less memory because of its compact internal representation.
- Maintains the natural order of enum constants.
- Provides compile-time type safety.
- Supports all standard Map operations.
- Allows null values while preventing null keys.
Limitations of EnumMap
- Can only be used with enum keys.
- Does not permit
nullkeys. - Not synchronized by default.
- Cannot be used when keys are not enums.
Methods
Method | Action Performed |
|---|---|
| clear() | Removes all mappings from this map. |
| clone() | Returns a shallow copy of this enum map. |
| containsKey?(Object key) | Returns true if this map contains a mapping for the specified key. |
| containsValue?(Object value) | Returns true if this map maps one or more keys to the specified value. |
| entrySet() | Returns a Set view of the mappings contained in this map. |
| equals?(Object o) | Compares the specified object with this map for equality. |
| get?(Object key) | Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. |
| hashCode() | Returns the hash code value for this map. |
| keySet() | Returns a Set view of the keys contained in this map. |
| put?(K key, V value) | Associates the specified value with the specified key in this map. |
| putAll?(Map<? extends K,?? extends V> m) | Copies all of the mappings from the specified map to this map. |
| remove?(Object key) | Removes the mapping for this key from this map if present. |
| size() | Returns the number of key-value mappings in this map. |
| values() | Returns a Collection view of the values contained in this map. |
Methods Declared in AbstractMap Class
Method | Description |
|---|---|
| isEmpty() | Returns true if this map contains no key-value mappings. |
| toString() | Returns a string representation of this map. |
Methods Declared in Interface java.util.Map
Method | Descriptionenter |
|---|---|
| compute?(K key, BiFunction<? super K,?? super V,?? extends V> remappingFunction) | Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). |
| computeIfAbsent?(K key, Function<? super K,?? extends V> mappingFunction) | If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null. |
| computeIfPresent?(K key, BiFunction<? super K,?? super V,?? extends V> remappingFunction) | If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value. |
| forEach?(BiConsumer<? super K,?? super V> action) | Performs the given action for each entry in this map until all entries have been processed or the action throws an exception. |
| getOrDefault?(Object key, V defaultValue) | Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key. |
| merge?(K key, V value, BiFunction<? super V,?? super V,?? extends V> remappingFunction) | If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. |
| putIfAbsent?(K key, V value) | If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value. |
| remove?(Object key, Object value) | Removes the entry for the specified key only if it is currently mapped to the specified value. |
| replace?(K key, V value) | Replaces the entry for the specified key only if it is currently mapped to some value. |
| replace?(K key, V oldValue, V newValue) | Replaces the entry for the specified key only if currently mapped to the specified value. |
| replaceAll?(BiFunction<? super K,?? super V,?? extends V> function) | Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception. |
EnumMap vs EnumSet
Property | EnumMap | EnumSet |
|---|---|---|
| Internal Representation | EnumMap is internally represented as arrays. The representation is compact and efficient. | EnumSet is internally represented as BitVector or sequence of bits. |
| Permits Null Elements? | Null keys are not allowed but Null values are allowed. | Null elements are not permitted. |
| Is the Abstract Class? | No | Yes |
| Instantiation | Since EnumMap is not an abstract class, it can be instantiated using the new operator. | It is an abstract class, it does not have a constructor. Enum set is created using its predefined methods like allOf(), noneOf(), of(), etc. |
| Implementation | EnumMap is a specialized Map implementation for use with enum type keys. | EnumSet is a specialized Set implementation for use with enum types. |