Static HashMap Initialization

16,026

Solution 1

There are a few approaches to doing this. For example if your map is immutable you could consider using the Google Guava libraries It has an ImmutableMap class which could be used to construct your map as:-

static final ImmutableMap<String, Integer> WORD_TO_INT =
       new ImmutableMap.Builder<String, Integer>()
           .put("one", 1)
           .put("two", 2)
           .put("three", 3)
           .build();

If you are already using the Spring Framework and wiring your beans using XML, then you could fill the map in directly via the XML as :-

    ...    
<!-- creates a java.util.Map instance with the supplied key-value pairs -->
<util:map id="emails">
    <entry key="pechorin" value="[email protected]"/>
    <entry key="raskolnikov" value="[email protected]"/>
    <entry key="stavrogin" value="[email protected]"/>
    <entry key="porfiry" value="[email protected]"/>
</util:map>
    ...

Solution 2

Either you can use Guava library. But if you dont want to use 3rd party library, then there are two ways to do it:

  1. Static initializer

    private static final Map<String,String> myMap = new HashMap<String, String>();
    static {
         myMap.put(key1, value1);
         myMap.put(key2, value2);
    }
    
    public static Map getMap() {
         return Collections.unmodifiableMap(myMap);
    }
    
  2. Instance initialiser (anonymous subclass).

    private static final Map<String,String> myMap = new HashMap<String, String>()
    {
       {
         put(key1, value1);
         put(key2, value2);
       }
    };
    
    public static Map getMap() {
         return Collections.unmodifiableMap(myMap);
    }
    
    private static void addPair(String key, String val){
         // Add key val to map
    }
    

Suppose later on you want to add some constant to map, then you can do that as well.

Collections.unmodifiableMap: It helps to have a unmodifiable view of map, which can not be modified. As it gives unsupported-Exception, if any modification is made to map.

Share:
16,026
Marco Aviles
Author by

Marco Aviles

Java and Ruby developer. Always looking to learn more

Updated on June 07, 2022

Comments

  • Marco Aviles
    Marco Aviles about 2 years

    I have this class, I want to know:
    1° Is this the best way to define a static HashMasp
    2° Is this the best way to do it in a Spring based app?(does Spring offer a better way to do this?)

    Thanks in advance!

        public class MyHashMap {
            private static final Map<Integer, String> myMap;
            static {
                Map<CustomEnum, String> aMap = new HashMap<CustomEnum, String>();
                aMap.put(CustomEnum.UN, "one");
                aMap.put(CustomEnum.DEUX, "two");
                myMap = Collections.unmodifiableMap(aMap);
            }
    
            public static String getValue(CustomEnum id){
                return myMap.get(id);
            }
        }
    
    
        System.out.println(MyHashMap.getValue(CustomEnum.UN));