I\'m trying to weigh the pros and cons of using an EnumMap
over a HashMap
. Since, I will always be looking up using a String
, it seems tha
If the set of possible keys is finite and known in advance (as your example/question suggest), then the enum is a perfect representation of this. As other said, the use of an enum ensures that no mistake will be made in the use of the key.
Furthermore, this implementation of Map is quite optimized, as the range of the keys is known in advance (as far as I knwow, the EnumMap uses an array of length numberOfEnums internally, indexed by the enum's ordinal).
So I would also recommend EnumMap
.
Two (little) things to keep in mind though :