EnumMap or HashMap if lookup key is a String

后端 未结 4 1917
花落未央
花落未央 2021-02-03 21:45

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

4条回答
  •  Happy的楠姐
    2021-02-03 22:44

    If all valid keys can be enumerated, I would use that as it ensures you are always working with a valid value.

    It can also avoid confusion as String can be used for lots of things and is easy to turn an "Animal" string into a string used for something else. As enum types are not interchangable with other types in general (unless you use a common interface), there is less chance of error in coding.

提交回复
热议问题