title: HashMap java.util.ConcurrentModificationException
body: <p>i have the following code:</p>
  
  <pre><code>public List&lt;String&gt; processMap(Map&lt;String, String&gt; aMap) {
      Cloner cloner = new Cloner();
  
      Map&lt;String, String&gt; tempMap = cloner.deepClone(aMap);
  
      while(!tempMap.isEmpty()) {
          Iterator&lt;Entry&lt;String, String&gt;&gt; iterator = tempMap.entrySet().iterator();
  
          while(iterator.hasNext()) {
              Entry&lt;String, String&gt; entry = iterator.next(); // !!!
          }
      }
  
      return null;
  }
  </code></pre>
  
  <p>To deep copy the map i use this library: <a href="https://code.google.com/p/cloning/" rel="nofollow">Cloner</a></p>
  
  <p>I marked the line where i unfortunately get a <code>ava.util.ConcurrentModificationException</code> with '!!!'</p>
  
  <p>Can you please tell me why i get this exception?</p>
  
  <p>The complete code:</p>
  
  <pre><code>    Cloner cloner = new Cloner();
  
      Map&lt;String, FreebaseType&gt; tempFreebaseTypes = new HashMap&lt;String, FreebaseType&gt;();
      Map&lt;String, FreebaseType&gt; freebaseTypesCopy = cloner.deepClone(freebaseTypes);
  
      while(!freebaseTypesCopy.isEmpty()) {
          Iterator&lt;Entry&lt;String, FreebaseType&gt;&gt; iterator = freebaseTypesCopy.entrySet().iterator();
  
          while(iterator.hasNext()) {
              Entry&lt;String, FreebaseType&gt; entry = iterator.next();
  
              if(tempFreebaseTypes.containsKey(entry.getValue().getSuperType()) || entry.getValue().getSuperType() == null) {
                  tempFreebaseTypes.put(entry.getValue().getType(), entry.getValue());
  
                  freebaseTypesCopy.remove(entry.getKey());
              } 
          }
      }
  
      List&lt;FreebaseType&gt; sortedFreebaseTypes = new ArrayList&lt;FreebaseType&gt;();
      Iterator&lt;Entry&lt;String, FreebaseType&gt;&gt; iterator = tempFreebaseTypes.entrySet().iterator();
  
      while(iterator.hasNext()) {
          Entry&lt;String, FreebaseType&gt; entry = iterator.next();
  
          sortedFreebaseTypes.add(entry.getValue());
      }
  
      return sortedFreebaseTypes;
  </code></pre>