title: Thread safe Lazy loading of a static map
body: <p>I'm trying to lazily initialize a map in a thread safe way. I've come up with this following the <a href="https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom" rel="nofollow">initialization-on-demand holder idiom</a>:</p>
  
  <pre><code>private static class NameIndexMapHolder {
      private static final Map&lt;String, Long&gt; NAME_INDEX_MAP;
      static {
          Map&lt;String, Long&gt; map = new HashMap&lt;&gt;();
          map.put("John", 3424534643);
          map.put("Jane", 4328759749);
          NAME_INDEX_MAP = Collections.unmodifiableMap(map);
      }
  }
  
  public static Map&lt;String, Long&gt; getNameIndexMap() {
      return NameIndexMapHolder.NAME_INDEX_MAP;
  }
  </code></pre>
  
  <p>Does this work? Is it thread safe? From what I've read this works for Singletons only. The only other alternative I've read is double checked locking which seems to have its own issues.</p>