Java EnumMap

Java EnumMap class

Last Updated : 29 Mar 2026

Java EnumMap is used to store key-value pairs where the keys are enum types, providing a highly efficient and compact map implementation.

In this chapter, you will learn about the Java EnumMap class, its features, and how it works with enum keys.

What is Java EnumMap?

Java EnumMap is a specialized implementation of the Map interface designed specifically for enum keys. It extends the AbstractMap class and provides a fast and efficient way to store mappings where keys are enum constants. It is internally represented using arrays which makes it more efficient than other Map implementations when working with enums.

EnumMap Class Hierarchy

In the hierarchy, EnumMap extends the AbstractMap class and implements the Map interface, which is a part of the Java Collections Framework. The Map interface is a core interface that allows storing key-value pairs, and this hierarchy enables EnumMap to provide a specialized and efficient implementation for enum keys.

The hierarchy of the EnumMap class is shown in the figure below:

EnumMap class hierarchy

EnumMap class declaration

Let's see the declaration for java.util.EnumMap class.

EnumMap class Parameters

Let's see the Parameters for java.util.EnumMap class.

  • K: It is the type of keys maintained by this map.
  • V: It is the type of mapped values.

Constructors of Java EnumMap class

Java EnumMap provides constructors to create and initialize an enum map in different ways.

1. EnumMap(ClasskeyType)

This constructor creates an empty EnumMap with the specified key type.

Here is syntax:

2. EnumMap(EnumMapm)

It creates an EnumMap with the same key type and mappings as the specified EnumMap.

Here is syntax:

3. EnumMap(Mapm)

It creates an EnumMap initialized with the mappings from the specified map.

Here is syntax:

Methods of EnumMap Class

The following table lists the commonly used methods of the EnumMap class along with their descriptions.

SNMethodDescription
1clear()It is used to clear all the mapping from the map.
2clone()It is used to copy the mapped value of one map to another map.
3containsKey()It is used to check whether a specified key is present in this map or not.
4containsValue()It is used to check whether one or more key is associated with a given value or not.
5entrySet()It is used to create a set of elements contained in the EnumMap.
6equals()It is used to compare two maps for equality.
7get()It is used to get the mapped value of the specified key.
8hashCode()It is used to get the hashcode value of the EnumMap.
9keySet()It is used to get the set view of the keys contained in the map.
10size()It is used to get the size of the EnumMap.
11Values()It is used to create a collection view of the values contained in this map.
12put()It is used to associate the given value with the given key in this EnumMap.
13putAll()It is used to copy all the mappings from one EnumMap to a new EnumMap.
14remove()It is used to remove the mapping for the given key from EnumMap if the given key is present.

Examples of Java EnumMap Class

The following examples demonstrate how to use Java EnumMap to store and work with enum keys.

Example 1: Creating and Iterating EnumMap

This example demonstrates how to create an EnumMap, add elements, and iterate through it.

Output:

Monday 1
Tuesday 2
Wednesday 3
Thursday 4

Example 2: Storing Custom Objects in EnumMap

This example demonstrates how to store custom objects in an EnumMap.

Output:

101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6