Open In App

Difference Between EnumMap and HashMap

Last Updated : 24 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

EnumMap and HashMap both are the classes that implement the Map interface. But there are some differences that exist between them. So we have tried to list out the differences between EnumMap and HashMap.

1. EnumMap: EnumMap is a specialized implementation of the Map interface for enumeration types. It extends AbstractMap and implements the Map interface in Java. Few important points of EnumMap are as follows: 

  • EnumMap class is a member of the Java Collections Framework and it is not synchronized.
  • EnumMap is an ordered collection, and they are maintained in the natural order of their keys(the natural order of keys means the order on which enum constant are declared inside enum type).
  • EnumMap is much faster than HashMap.
  • All keys of each EnumMap instance must be keys of the same enum type.
  • EnumMap doesn’t allow inserting null key if we try to insert the null key, it will throw NullPointerException.
  • EnumMap is internally represented as arrays therefore it gives the better performance.

EnumMap Demo:

Java




// Java program to illustrate working
// of EnumMap
 
import java.util.*;
 
class EnumMapExample {
 
    public enum Days {
        Sunday,
        Monday,
        Tuesday,
        Wendnesday;
    }
 
    public static void main(String[] args)
    {
        // Creating an EnumMap of the Days enum
        EnumMap<Days, Integer> enumMap
            = new EnumMap<>(Days.class);
 
        // Insert using put() method
        enumMap.put(Days.Sunday, 1);
        enumMap.put(Days.Monday, 2);
        enumMap.put(Days.Tuesday, 3);
        enumMap.put(Days.Wednesday, 4);
 
        // Printing size of EnumMap
        System.out.println("Size of EnumMap: "
                           + enumMap.size());
        // Printing the EnumMap
        for (Map.Entry m : enumMap.entrySet()) {
            System.out.println(m.getKey() + " "
                               + m.getValue());
        }
    }
}


Output

Size of EnumMap: 4
Sunday 1
Monday 2
Tuesday 3
Wednesday 4

2. HashMap: HashMap is a class that implements the Map interface. It stores the data in the key and value pair, where the key should be unique. If you try to insert the duplicate key, it will replace the element of the corresponding key. It allows to store the null keys as well as null values, but there should be only one null key and multiple null values. Java HashMap is similar to HashTable, but it is synchronized. HashMap doesn’t maintain the order that means it doesn’t guarantee any specific order of the elements.

HashMap Demo:

Java




// Java program to illustrate
// the working of HashMap
 
import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
        // Create an empty hash map
        HashMap<Integer, String> map = new HashMap<>();
 
        // Adding elements to the map
        map.put(10, "Geeks");
        map.put(20, "Ram");
        map.put(30, "Shyam");
 
        // Printing size of the map
        System.out.println("Size of map is: " + map.size());
 
        // Iterating the map
        for (Map.Entry m : map.entrySet()) {
            System.out.println(m.getKey() + " "
                               + m.getValue());
        }
    }
}


Output

Size of map is: 3
20 Ram
10 Geeks
30 Shyam

Difference between EnumMap and HashMap

                              EnumMap                             HashMap
EnumMap is a specialized map implementation that uses only Enum type key. In HashMap, we can use Enum as well as any other object as a key.
It doesn’t allow storing null key. It allows to store the null keys as well values, but there should be only one null key object and there can be any number of null values.
EnumMap internally uses the array HashMap internally uses the HashTable.
EnumMap is a specialized map implementation that uses only enum type keys. Due to this, EnumMap is performed better than HashMap. Performance of HashMap is slightly less than compared to EnumMap.
EnumMap stores the keys in the natural order of their keys (order in which the enum constant are declared). In HashMap, no ordering of the keys.
Since EnumMap internally uses the array and stores the key in their natural ordering using ordinal(), so there is no probability of collision. HashMap uses the hashCode() to store key and values, so there is probability of collision.


Similar Reads

Difference Between EnumMap and EnumSet in Java
EnumMap and EnumSet both are the classes defined inside the java collection. In this article, we will learn the differences between EnumMap and EnumSet. EnumMap is the specialized implementation of the Map interface and the EnumSet is the specialized implementation of the Set interface. There are some differences that exist between them. So we have
3 min read
Difference Between IdentityHashMap, WeakHashMap, and EnumMap in Java
The IdentityHashMap, WeakHashMap, and EnumMap all are the classes in java collection that implements the Map interface. But there are few differences exists between them. 1. IdentityHashMap: The IdentityHashMap implements the Map interface. It follows reference-equality in place of object-equality when comparing keys (and values). This class is use
5 min read
How to Copy One HashMap to Another HashMap in Java?
HashMap is similar to the HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values. This class makes no guarantees as to the order of the map. To use this class and its methods, you need to import java.util.HashMap package or its superclass
4 min read
Converting Integer-String HashMap to String-Array-Integer HashMap in Java
Efficient Conversion of a HashMap&lt;Integer, String&gt; into another HashMap&lt;String, Integer[]&gt; involves organizing integer keys based on their corresponding string values. Here's a concise and efficient Java method to accomplish this transformation, focusing on clear and straightforward implementation. Step-by-Step Implementation of Convers
3 min read
EnumMap remove() Method in Java
The Java.util.EnumMap.remove(key) method in Java is used to remove the specified key from the map. Syntax: remove(Object key) Parameters: The method takes one parameter key which refers to the key whose mapping is to be removed. Return Value: The method does not return any value. Below programs illustrate the working of remove(key) function: Progra
2 min read
EnumMap size() Method in Java
The Java.util.EnumMap.size() method in java is used to know the size of the map or the number of elements present in the map. Syntax: Enum_Map.size() Parameters: The method does not take any parameters. Return Value: The method returns the size of the map. Below programs illustrate the working of size() function: Program 1: // Java program to demon
2 min read
EnumMap equals() Method in Java with Examples
The Java.util.EnumMap.equals(obj) in Java is used to compare the passed object with this EnumMap for the equality. It must be kept in mind that the object passed must be a map of the same type as the EnumMap.Syntax: boolean equals(Object obj) Parameter: The method takes one parameter obj of Object type and refers to the map to be compared with this
2 min read
EnumMap clear() Method in Java with Examples
The Java.util.EnumMap.clear() method in Java is used to remove all the mappings from the map. The method does not delete the map instead it just clears the map of the mappings. Syntax: enum_map.clear() Parameters: This method does not accept any argument. Return Values: This method does not return any value. Below programs illustrate the working of
2 min read
EnumMap entrySet() Method in Java
The Java.util.EnumMap.entrySet() method in Java used to create a set out of the elements contained in the EnumMap. It basically returns a set view of the enum map. Syntax: enum_map.entrySet() Parameter: The method does not take any parameters. Return Value: The method returns the set view of mappings contained in this enum_map. Below programs illus
2 min read
EnumMap clone() Method in Java
The Java.util.EnumMap.clone() method in Java is used to copy the mapped values of one map to another. It basically creates a shallow copy of this map. Syntax: Enum_map_2 = Enum_map_1.clone() Parameters: The method does not accept any argument. Return Value: The method returns a shallow copy of a EnumMap. Below programs illustrate the Java.util.Enum
2 min read