title: Map of AtomicInteger
body: <p>I want to implement a shared object for calculation of statistics of executions of operations.
  Objects state will be represented by <code>Map&lt;String,AtomicInteger&gt;</code>(key is the name of operation, value is the number of times the operation was executed). Am I correct that I can choose a <code>HashMap&lt;String,AtomicInteger&gt;</code> implementation and use no synchronization on it for getting values from it since AtomicInteger has a volatile <code>value</code> field underneath it. </p>
  
  <p>Sample of code that does addition and incrementation of execution stats:</p>
  
  <pre><code>import java.util.HashMap;
  import java.util.Map;
  import java.util.concurrent.atomic.AtomicInteger;
  
  public final class Stats {
  
  private final Map&lt;String, AtomicInteger&gt; statistics = new HashMap&lt;String, AtomicInteger&gt;();
  
  public int increment(String operationName) {
      if (!statistics.containsKey(operationName)) {
          synchronized (statistics) {
              if (!statistics.containsKey(operationName)) 
                  statistics.put(operationName, new AtomicInteger(0));
          }
      }
  
      return statistics.get(operationName).getAndIncrement();
  }
  
  public int getOpStats(String operationName) {
      if (!statistics.containsKey(operationName)) {
          return 0;
      }
      return statistics.get(operationName).get();
  }
  
  }
  </code></pre>