title: Copying ConcurrentHashMap to HashMap inconsistency
body: <p>im trying to do the following:</p>
  
  <ol>
  <li>My program starts threads, which increment an <code>AtomicInteger</code> from <code>ConcurrentHashMap&lt;String, AtomicInteger&gt;</code> and adds new <code>Integer</code> to <code>ConcurrentHashMap&lt;String, CopyOnWriteArrayList&lt;Integer&gt;&gt;</code>. In that case, the size of the <code>CopyOnWriteArrayList</code> is equal to the Value of the <code>AtomicInteger</code> (of course for entries with the same key)</li>
  <li>After all threads are done (when the <code>CountDownLatch</code> is finished) I try to convert <code>ConcurrentHashMap&lt;String, AtomicInteger&gt;</code> into <code>HashMap&lt;String, Integer&gt;</code> for sorting that Map by Value, because <code>AtomicInteger</code> is not comparable.</li>
  <li>After conversion I sort the <code>HashMap</code> by the Values and pick 20 entries with the highest value - in the sorted Map they are the first 20 entries.</li>
  <li>Finally I pack the Values in an List and make JSON String with GSON.</li>
  </ol>
  
  <p><strong>Issue</strong></p>
  
  <p>What I expect: due my usage of <code>AtomicInteger</code>, <code>ConcurrentHashMap</code> and <code>CopyOnWriteArrayList</code>  I expect that all sizes and values for all entries with the same key are equal, even in my JSON String like:</p>
  
  <pre><code>myAtomcIntegerConcurrentHashMap.get("ABC").intValue() == 
  myCOWArrayListConcurrentHashMap.get("ABC").size() == 
  myNewHashMap.get("ABC")
  </code></pre>
  
  <p>But result seems be different. I made some console outputs to test my Values and got the following:</p>
  
  <p>While copying from <code>ConcurrentHashMap</code> to <code>HashMap</code> I verify my values again. Every time the "bad copied" values are different (for the code snippet look below):
  </p>
  
  <pre><code> COWArrayList.size  AtomicInteger.intValue  Value in new HashMap
   299                299                     298
   122                122                     121
  </code></pre>
  
  <p>After that I iterate 4 more times over my new <code>HashMap</code> to compare my values again and every time I get new random "bad copied" values (notice, the values were not detected while copying) (for the code snippet look below):
  </p>
  
  <pre><code> COWArrayList.size  AtomicInteger.intValue  Value in new HashMap  Common Key
   849                849                     827                   CGCCACC
   838                838                     813                   GGTGGTG
  </code></pre>
  
  <p>My Json is also incorrect. E.g. for the Key <code>"CGCCACC"</code> Size of my Array in Json is <code>887</code>, not like in the Table above (<code>849</code>).</p>
  
  <p><strong>Here are code snippets I use</strong> (Some of them are from StackOverflow):</p>
  
  <p>Incrementing <code>AtomicInteger</code> and adding new Integer to <code>CopyOnWriteArrayList</code> in my Threads:</p>
  
  <pre class="lang-java prettyprint-override"><code>//Add new Integer 'position' to the COWArrayList from 'positions' with the key 'frame'
  List&lt;Integer&gt; copyArr = positions.get(frame);
  if (copyArr == null) {
    copyArr = new CopyOnWriteArrayList&lt;Integer&gt;();
    List&lt;Integer&gt; inMap = positions.putIfAbsent(frame, (CopyOnWriteArrayList&lt;Integer&gt;) copyArr);
    if (inMap != null) copyArr = inMap; // already in map
  }
  copyArr.add(position);
  
  //Increment the AtomicInteger from 'scores' with the key 'frame'
  AtomicInteger value = scores.get(frame);
  if (value==null){ 
    value = new AtomicInteger();
    AtomicInteger actual = scores.putIfAbsent(frame, value);
    if(actual != null) value = actual;
  }
  value.incrementAndGet();
  </code></pre>
  
  <p>Copy from <code>ConcurrentHashMap&lt;String, AtomicInteger&gt;</code> to the <code>HashMap&lt;String, Integer&gt;</code> each value (I guess its very inefficient) and immediate verifying:</p>
  
  <pre class="lang-java prettyprint-override"><code>//init new, non-concurrent Map
  Map&lt;String, Integer&gt; myHashMap = new HashMap&lt;String, Integer&gt;();
  
  //iterate over the Map and copy each value from 'scores' to 'newHashMap'
  for(Map.Entry&lt;String, AtomicInteger&gt; score : scores.entrySet()){
    myHashMap.put(score.getKey(), score.getValue().intValue());
  
    //verify just added Value and print values of all Maps if something is wrong
    if(score.getValue().intValue() != myHashMap.get(score.getKey())){
      System.out.println(score.getValue().intValue() + " " + positions.get(score.getKey()).size() + " " + myHashMap.get(score.getKey()));
    }
  }
  </code></pre>
  
  <p>Verifying copied values of <code>myHashMap</code> again (here I also get random "bad copied" values):</p>
  
  <pre class="lang-java prettyprint-override"><code>for(Map.Entry&lt;String, AtomicInteger&gt; score : scores.entrySet()){
    if(score.getValue().intValue() != myHashMap.get(score.getKey())){
    System.out.println(score.getValue().intValue() + " = " + positions.get(score.getKey()).size() + " =? " + myHashMap.get(score.getKey()));
    }
  }
  </code></pre>
  
  <p>Why could that happen, have I missed something in my logic?</p>
  
  <p>For further information / code etc. - just ask.</p>
  
  <p><strong>Thank you for helping me!</strong></p>