Difference Between EnumMap and HashMap Last Updated : 24 Nov, 2022 Comments Improve Suggest changes Like Article Like 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()); } } } OutputSize of EnumMap: 4 Sunday 1 Monday 2 Tuesday 3 Wednesday 42. 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()); } } } OutputSize of map is: 3 20 Ram 10 Geeks 30 ShyamDifference between EnumMap and HashMap EnumMap HashMapEnumMap 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 arrayHashMap 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. Comment More infoAdvertise with us Next Article Difference Between EnumMap and HashMap prashant_srivastava Follow Improve Article Tags : Java Technical Scripter Difference Between Technical Scripter 2020 Java-Collections Java-HashMap Java-EnumMap +3 More Practice Tags : JavaJava-Collections Similar Reads Difference between HashMap and HashSet HashSet is an implementation of Set Interface which does not allow duplicate value. The main thing is, objects that are stored in HashSet must override equals() for check for equality, and hashCode() methods for no duplicate value are stored in our set. HashMap is an implementation of Map Interface, 4 min read 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 so 3 min read Difference between HashMap and IdentityHashMap in Java HashMap in java is a class that is a part of the java collection. It implements the Map interface of java. It stores the data in the form of key and value pair. The key should be unique but the value may be duplicate. If you try to insert the duplicate key, it will replace the element of the corresp 4 min read Differences between HashMap and HashTable in Java HashMap and Hashtable store key and value pairs in a hash table. When using a Hashtable or HashMap, we specify an object that is used as a key and the value that you want to be linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored w 3 min read Difference between List, Set and Map in Java List interface in Java is a sub-interface of the Java collections interface. It contains the index-based methods to insert, update, delete, and search the elements. It can have duplicate elements also. We can also store the null elements in the list. List preserves the insertion order, it allows pos 4 min read Difference Between EnumSet and TreeSet in Java EnumSet and TreeSet both are the classes defined inside the collection framework. But there are few differences exists between them. In this article, we have tried to cover all these differences between them. 1. EnumSet: EnumSet is a specialized implementation of the Set interface for enumeration ty 3 min read Difference between HashMap and ConcurrentHashMap HashMap is the Class which is under Traditional Collection and ConcurrentHashMap is a Class which is under Concurrent Collections, apart from this there are various differences between them which are: HashMap is non-Synchronized in nature i.e. HashMap is not Thread-safe whereas ConcurrentHashMap is 3 min read Differences between TreeMap, HashMap and LinkedHashMap in Java Prerequisite: HashMap and TreeMap in Java TreeMap, HashMap and LinkedHashMap: What's Similar? All offer a key->value map and a way to iterate through the keys. The most important distinction between these classes is the time guarantees and the ordering of the keys.All three classes HashMap, TreeM 5 min read Difference Between keySet() and entrySet() Method in Java Map Map Interface is present in Java.util package, which provides mainly three methods KeySet(),entrySet() and values(). These methods are used to retrieve the keys of the map, key-value pairs of the map, and values of the map respectively. Since these methods are part of Map Interface, so we can use th 4 min read What is the difference between Hashing and Hash Tables? What is Hashing? Hashing refers to the process of generating a fixed-size output from an input of variable size using the mathematical formulas known as hash functions. This technique determines an index or location for the storage of an item in a data structure. It might not be strictly related to 2 min read Like