HashMap
vs EnumMap in Java
What is difference between EnumMap and HashMap in Java is the latest
Java collection interview question which has been asked to couple of my
friends. This is one of the tricky
Java question, specially if you are not very much familiar with EnumMap
in Java, which is not uncommon, given you can use it with only Enum
keys. Main difference between EnumMap
and HashMap is that EnumMap is a specialized Map implementation exclusively
for Enum as key. Using Enum as key, allows to do some implementation level
optimization for high performance which is generally not possible with other
object's as key. We have seen 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 to some of my friend. Unlike HashMap, EnumMap is not applicable
for every case but its best suited when you have Enum as key. We have already
covered basics of EnumMap and some EnumMap example in my last article What
is EnumMap in Java and In this post we will focus on key differences
between HashMap and EnumMap in Java.