title: How to remove a key from HashMap while iterating over it?
body: <p>I am having <code>HashMap</code> called <code>testMap</code> which contains <code>String, String</code>.</p>
  
  <pre><code>HashMap&lt;String, String&gt; testMap = new HashMap&lt;String, String&gt;();
  </code></pre>
  
  <p>When iterating the map, if <code>value</code> is match with specified string, I need to remove the key from map.</p>
  
  <p>i.e.</p>
  
  <pre><code>for(Map.Entry&lt;String, String&gt; entry : testMap.entrySet()) {
    if(entry.getValue().equalsIgnoreCase("Sample")) {
      testMap.remove(entry.getKey());
    }
  }
  </code></pre>
  
  <p><code>testMap</code> contains <code>"Sample"</code> but I am unable to remove the key from <code>HashMap</code>. <br>Instead getting error :</p>
  
  <pre><code>"Exception in thread "main" java.util.ConcurrentModificationException
      at java.util.HashMap$HashIterator.nextEntry(Unknown Source)
      at java.util.HashMap$EntryIterator.next(Unknown Source)
      at java.util.HashMap$EntryIterator.next(Unknown Source)"
  </code></pre>