2

I have a map data already with me in string format for eg -

in = ch.ed, 
au = nsw.syd, 
us = ny.nj, 
ca = on.to,

Can someone please suggest which data structure i should use in java to put this data in. "Enummap" or a "hashmap" or if any other suggestions ? What are advantages if i use "enummap". My map will remain unchanged throughout the package.

Thanks

Vegard
  • 4,802
  • 1
  • 20
  • 30
user2577756
  • 79
  • 1
  • 7

3 Answers3

3

Main difference between EnumMap and HashMap is that EnumMap is a specialized Map implementation exclusively for Enum as key.

So If your keys are the type Enum go for a EnumMap otherwise Go for HashMap.

There are some advantages of having ENUM as a key,Because

Key Of Map should be unmodifiable and Unique and this can be guranteed using Enum.

Prefer to read : EnumMap or HashMap if lookup key is a String

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

If you want the sorted map then use Tree Map.

If requirement is to iterate on the entries in the same order as they were put,consider LinkedHashMap

Otherwise always prefer the HashMap if you dont care about any of the above criteria

EnumMap is a specialized Map implementation for use with enum type Keys.see http://docs.oracle.com/javase/7/docs/api/java/util/EnumMap.html.So if your keys are of Enum types go for it.

M Sach
  • 33,416
  • 76
  • 221
  • 314
0

You can go for static HashMap which can be initialized and filled at the time of application start up. Other classes can only get the values from Map. You can wrap that map around a bean and only that bean will be accessible by all classes. What I am trying to tell you is, you should go for singleton design pattern.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102