Difference Between EnumMap and HashMap in Java



In this article, we will learn about the EnumMap and HashMap in Java. First, we will know about EnumMap, the syntax of EnumMap, and an example after that will learn about HashMap, the syntax of HashMap, and an example. At the end, we will see a table showing the difference between EnumMap and HashMap.

What is EnumMap?

EnumMap is introduced in JDK 5. It is designed to use an Enum as a key in the Map. It is an implementation of the Map interface.

All of the keys in the EnumMap should be of the same enum type. Keys cannot be null; doing so will throw a NullPointerException.

As per Java docs:
EnumMap internally uses arrays, making this representation extremely compact and efficient.

Syntax

Below is the syntax for declaring an EnumMap in Java:

public EnumMap(Class<K> keyType)

Where keyType is the class object of the key type for this enum map.

Example of EnumMap

Below is an example of creating and adding elements to an EnumMap in Java:

import java.util.EnumMap;
import java.util.Map;
public class EnumMapExample {
   public enum LaptopEnum {
      HCL, DELL, IBM
   };
   public static void main(String[] args) {
      // create enum map
      EnumMap<LaptopEnum, String> map = new EnumMap<>(LaptopEnum.class);
      map.put(LaptopEnum.HCL, "100");
      map.put(LaptopEnum.DELL, "200");
      map.put(LaptopEnum.IBM, "300");
      // print the map
      for (Map.Entry<LaptopEnum, String> m : map.entrySet()) {
         System.out.println(m.getKey() + " " + m.getValue());
      }
   }
}

Output

HCL 100
IBM 300
DELL 200

What is a HashMap?

HashMap is also an implementation of the Map interface. It is used to store data in key-value pairs.

It can contain one null key and multiple null values. In HashMap, keys cannot be of a primitive type.

Java HashMap provides constant-time performance for basic operations (get and put), assuming the hash function disperses the elements properly among buckets.

Syntax

Below is the syntax for declaring a HashMap in Java:

public HashMap(Map m)

This constructor initializes the hash map using the elements of the given Map object m.

Example of HashMap

Below is an example of creating and adding elements to a HashMap in Java:

import java.util.HashMap;
import java.util.Map;
public class HashMapExample {
   public static void main(String[] args) {
      // create Hash map
      Map<String, String> map = new HashMap<>();
      map.put("HCL", "100");
      map.put("DELL", "200");
      map.put("IBM", "300");
      // print the map
      for (Map.Entry<String, String> m : map.entrySet()) {
         System.out.println(m.getKey() + " " + m.getValue());
      }
   }
}

Output

HCL 100
IBM 300
DELL 200

Difference between EnumMap and HashMap

Below is the comparison table of EnumMap and HashMap:

Key
EnumMap
HashMap
Basic A specialized Map implementation for use with enum type keys HashMap is also the implementation of the Map interface.
Null Key It can't have a null key. It can have one null key and multiple null values
Performance All operations execute in constant time therefore, it is faster than HashMap It is slower than HashMap
Internal Implementation It uses an Array internally It uses a Hashtable internally
Ordering EnumMap stores keys in the natural order of their keys HashMap is not ordered
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-06-16T16:40:48+05:30

848 Views

Kickstart Your Career

Get certified by completing the course

Get Started