title: How to avoid ConcurrentModificationException while iterating over map.entrySet
body: <p>I have the following code:</p>
  
  <pre><code>static final Map&lt;String, String&gt; map = Collections.synchronizedMap(new HashMap());
  
      public static void main(String[] args) throws InterruptedException {
          map.put("1", "1");
          map.put("2", "2");
          new Thread(new Runnable() {
              @Override
              public void run() {
                  for (Map.Entry&lt;String, String&gt; stringStringEntry : map.entrySet()) {
                      System.out.println("after iterator");
                      try {
                          Thread.sleep(1000);
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
  
                  }
              }
          }).start();
  
          Thread.sleep(50);
          map.remove("1");
          System.out.println("removed");
      }
  </code></pre>
  
  <p>It produces <code>java.util.ConcurrentModificationException</code></p>
  
  <p>How can I avoid it ?</p>