Why use EnumMap instead of HashMap

Ashish Choudhary picture Ashish Choudhary · Nov 21, 2018 · Viewed 9.8k times · Source

As we already have HashMap, why would we use EnumMap?

Answer

Thilo picture Thilo · Nov 21, 2018

The Javadoc makes a pretty good argument:

Enum maps are represented internally as arrays. This representation is extremely compact and efficient.

Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts.