Tuesday, August 3, 2021

Difference between EnumMap and HashMap in Java? Example

HashMap vs EnumMap in Java
What is the difference between EnumMap and HashMap in Java is the latest Java collection interview question which has been asked to a couple of my friends? This is one of the tricky Java questions, especially if you are not very much familiar with EnumMap in Java, which is not uncommon, given you can use it with only Enum keys. The main difference between EnumMap and HashMap is that EnumMap is a specialized Map implementation exclusively for Enum as key. Using Enum as a key allows doing some implementation level optimization for high performance which is generally not possible with other objects as key.

We have seen a lot of interview questions on HashMap in our article How HashMap works in Java but what we missed there is this question which is recently asked by some of my friends. Unlike HashMap, EnumMap is not applicable for every case but it's best suited when you have Enum as key. 

We have already covered the basics of EnumMap and some EnumMap examples in my last article What are EnumMap in Java and In this post, we will focus on key differences between HashMap and EnumMap in Java.



EnumMap vs HashMap

Difference between HashMap vs EnumMap in JavaBefore looking at the differences between EnumMap and HashMap, few words about What is common between them. Both of them implements Map interface so they can be used in all methods which accept Map and data can be accessed using common Map methods e.g. get() and put(). Internally EnumMap is represented using Array and provides constant-time performance for common methods e.g. get() or put(). Now let's see few differences between EnumMap vs HashMap :



1) As said earlier, the first and foremost difference between EnumMap and HashMap is that EnumMap is optimized for enum keys while HashMap is a general-purpose Map implementation similar to Hashtable. you can not use any type other than Enum as a key in EnumMap but you can use both Enum and any other Object as a key in HashMap.

2) Another difference between EnumMap and HashMap is performance. as discussed in the previous point, due to specialized optimization done for Enum keys, EnumMap is likely to perform better than HashMap when using an enum as a key object.

3) One more thing which can be considered as the difference between HashMap and EnumMap is the probability of Collision. Since Enum is internally maintained as an array and they are stored in their natural order using ordinal(), as shown in the following code which is taken from the put() method of EnumMap

 int index = ((Enum)key).ordinal();
 Object oldValue = vals[index];
 vals[index] = maskNull(value);

Since EnumMap doesn't call the hashCode method on keys, there is no chance of collision.

These were some notable differences between EnumMap and HashMap in Java. In short, EnumMap is best suited for enum keys, for which it has optimized and performed better than HashMap in Java. Use EnumMap whenever you can use enum as keys.

Other Interview Questions from Javarevisited Blog

1 comment:

  1. Hello there.

    Congratulations on posting (somewhat) original content. What would be even more useful though is the pointing out of the well-known (to those who write Java for 3+ years) issues with EnumMap's entrySet: http://stackoverflow.com/questions/6198188/iterating-over-enummapentryset

    ReplyDelete