Difference Between EnumMap and HashMap - GeeksforGeeks

28 Dec, 2020
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,        Wendensday;    }      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.Wendensday, 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());        }    }}
Size of EnumMap: 4
Sunday 1
Monday 2
Tuesday 3
Wendensday 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());        }    }}
Size of map is: 3
20 Ram
10 Geeks
30 Shyam
Java-Collections
Java-EnumMap
Java-HashMap
Technical Scripter 2020
Difference Between
Java
Technical Scripter
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Informed and Uninformed Search in AI
Difference between HashMap and HashSet
Difference between Internal and External fragmentation
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Reverse a string in Java
Arrays.sort() in Java with examples