title: Why do I get a ConcurentModificationException when removing from a HashMap?
body: <p>I want to remove an item from HashMap, by applying a criteria. Consider this code:</p>
  
  <pre><code>Set&lt;Foo&gt; set = myMap.keySet();
  Iterator&lt;Foo&gt; itr = set.iterator();
  while (itr.hasNext())
  {
      Foo foo = itr.next();
      if (foo.toString().length() &lt; 3) {
          myMap.remove(foo); //remove the pair if key length is less than 3
      }
  }
  </code></pre>
  
  <p>So I get a ConcurentModificationException because during the iteration I am modifying the HashMap. What should I do? Is there any other way to search for my criteria and execute the remove command at the end so that I can avoid this exception?</p>