title: Populating singleton Map
body: <p>I have the following code:</p>
  
  <pre><code>public class SomeClass{
  
    private static volatile TreeMap&lt;String, SomeData&gt; map;
  
    public static TreeMap&lt;String,String&gt; getSingletonMap(){
  
        if(map=null){
          synchronized(SomeClass.class){
            if(map==null){
               map = new TreeMap&lt;&gt;();
             }
          }
         }
       return map;
     }
  
   public static synchronized void add(SomeData data){
       TreeMap&lt;String,String&gt; treeMap = getSingletonMap();
       treeMap.put(data.key, data);
   }
  
   public final class SomeData{
       String key;
       public SomeData(String key){
          this.key = key;
       }
  
   }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>public class SomeOtherClass{
  
      //this method is called by multiple threads
      public static synchronized collectData(){
          try{
               //do some iteration here and add data at every iteration
               for(String str : strings){
                   SomeClass.add(new SomeClass.SomeData(string));
                }
           }
           finally{
             //do some processing of collected data by main Thread
             // at this point TreeMap of SomeClass is null;
           }
       }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Every time different thread is accessing the loop and adding data, the TreeMap is initialised again. And in the finally block, when the main thread continues, it also gets initialised again, but the TreeMap is supposed to be a singleton. What am I missing here? Any ideas? </p>