What is EnumMap in Java
EnumMap in Java is added on JDK 5 release along with other important features like Autoboxing, varargs and Generics. EnumMap is specialized Map implementation designed and optimized for using Java Enum as key. Since enum can represent a type (like class or interface) in Java and it can also override equals() and hashCode() , It can be used inside HashMap or any other collection but using EnumMap brings implementation specific benefits which is done for enum keys, In short EnumMap is optimized Map implementation exclusively for enum keys . As per javadoc Enum is implemented using Arrays and common operations result in constant time. So if you are thinking of an high performance Map, EnumMap could be decent choice for enumeration data. We have already seen many examples of Java enum in our article 10 Examples of enum in Java and using Enum as thread-safe Singleton. In this Java tutorial we will see simple examples of using EnumMap in Java. On related note Difference between EnumMap and HashMap is also getting popular as one of advanced Java collection interviews questions and most of Java IDE like Eclipse and Netbeans will also suggest to use EnumMap if you use Enum keys along with HashMap as part of there code improvement suggestion.