5 Difference between EnumMap and HashMap with Example

One of my readers asked me to write the difference between EnumMap and HashMap. EnumMap is not heavily used as compare to HashMap in real world applications. But EnumMap has some advantages like performance which make it a preferred collection to use over others. In this article I will share the difference between HashMap and EnumMap, example of EnumMap and HashMap plus similarities between them.

Read Also: How HashMap works in java


Difference between EnumMap and HashMap in Java

1. Key : In EnumMap keys are enum type.The syntax is EnumMap,V>. In other words, you can not use any type other than Enum as key in EnumMap.
In HashMap, there is no constraint. You can use Enum as well as any other Object as key.

2. Performance : EnumMap is a specialized Map implementation for use with enum type keys. EnumMaps are represented internally as arrays. This representation is extremely compact and efficient. Due to this, EnumMap are faster than the HashMap. 

3. Internal implementation : EnumMaps are represented internally as arrays. While, HashMap is Hashtable based implementation.

4. Probability of Collision : EnumMap put() method internally calls ordinal() method of Enum class to maintain the natural order of  Enum keys. Since, there is no call of hashCode() method on keys , hence there is no hash collision.

5. Ordering : In EnumMaps are maintained in the natural order of their keys (the order in which the enum constants are declared). This is reflected in the iterators returned by the collection views( keySet(), entrySet() and values()).
In HashMap there is no ordering of the keys.
 

Example of EnumMap and HashMap

Below you will find the examples of EnumMap and HashMap. First look at the EnumMap example.

import java.util.EnumMap; 
  
public class JavaHungry 
{ 
    public enum Fruit 
    { 
        BANANA, APPLE, ORANGE, MANGO; 
    } 
  
    public static void main(String args[])  
    {     
        // Java EnumMap Example
        // Creating EnumMap in java with key   
        // as enum type STATE 
        EnumMap<Fruit, String> fruitMap = new 
                     EnumMap<Fruit, String>(Fruit.class); 
  
        // Java EnumMap Another Example: 
        // Putting values inside EnumMap in Java 
        // Inserting Enum keys different from  
        // their natural order 
        fruitMap.put(Fruit.ORANGE, "Orange is delicious"); 
        fruitMap.put(Fruit.APPLE, "Apple is delicious"); 
        fruitMap.put(Fruit.MANGO, "Mango is delicious"); 
        fruitMap.put(Fruit.BANANA, "Banana is delicious"); 
          
        // Displaying size of EnumMap in java 
        System.out.println("Size of EnumMap : " +  
                                       fruitMap.size()); 
       
        // Printing Java EnumMap  
        // Print EnumMap in natural order 
        // of enum keys (order on which they are declared) 
        System.out.println("EnumMap : " + fruitMap); 
       
        // Fetch value from EnumMap in java 
        System.out.println("Key : " + Fruit.MANGO +" Value: " 
                                   + fruitMap.get(Fruit.MANGO)); 
    } 
} 

Output :
Size of EnumMap : 4
EnumMap : {BANANA=Banana is delicious, APPLE=Apple is delicious, ORANGE=Orange is delicious, MANGO=Mango is delicious}
Key : MANGO Value: Mango is delicious

Now, Look at the HashMap example

import java.util.Hashtable;
import java.util.HashMap;

public class HashMapExample {
    
    public static void main(String[] args) { 

        HashMap hashmapobj = new HashMap();
        hashmapobj.put("Apple", "Apple is delicious");  
        hashmapobj.put("Mango", "Mango is delicious"); 
        System.out.println("HashMap object output :"+hashmapobj);   
 }
}

Output :
HashMap object output :{Apple=Apple is delicious, Mango=Mango is delicious}

Similarities between EnumMap and HashMap

1. Both EnumMap and HashMap implements Map interface. Both classes can access the data through get() and put() method provided by the Map interface.

2. EnumMap and HashMap can store the null values.

3. EnumMap and HashMap are not synchronized.

4. Both are member of the java collections framework.

Recap : Difference between EnumMap and HashMap in Java


EnumMapHashMap
Keyshould be Enum typeAny Object can be key including enum
PerformanceFaster than HashMap Slower than EnumMap
Internal implementationStored as arraysHashtable based implementation
Probability of CollisionNo Yes
OrderingNatural ordering of keys
(order in which enum constants
are decalred)
No ordering of keys

That's all for difference between EnumMap and HashMap in java topic. If you have any questions regarding EnumMap or HashMap then please mention in the comments.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry