title: HashMap iteration/removal getting java.util.ConcurrentModificationException
body: <p>i have a HashMap of ClientSocket and Client Object.</p>
  
  <p>i'm iterating throw them using for loop, but sometimes new lines are added to the hashmap and then i get the java.util.ConcurrentModificationException error.
  i accutally understand why it happens, but i don't understand how to solve it. i've tried to make a new copy of my HashMap before the iteration starts but yet - i still got the error.</p>
  
  <p>my code:</p>
  
  <pre><code>private volatile HashMap&lt;ClientSocket, Client&gt; clientsMap = new HashMap&lt;ClientSocket, Client&gt;();
  private volatile HashMap&lt;ClientSocket, Client&gt; iteratorClientsMap = new HashMap&lt;ClientSocket, Client&gt;();
  private volatile ClientsMapIterator iterator;
  
  iterator = new ClientsMapIterator(clientsMap);
  iteratorClientsMap = iterator.getItreator();
  
  for (Map.Entry&lt;ClientSocket, Client&gt; entry : iteratorClientsMap.entrySet()) {                                                                   
      ClientSocket key = entry.getKey();
      //Client value = entry.getValue();              
      long diff = currentTime - key.getLastOnline();
      boolean isAvailable = false;
  
      try {
          isAvailable = (key.getSocket().getInputStream().available() &gt; 0);
      } catch (IOException e) {
          e.printStackTrace();
      }               
  
      if ( diff &gt; keepAlive)              
          removeClientSocket(key);
  }
  
  public synchronized void addClientSocket(ClientSocket clientSocket) {
      clientsMap.put(clientSocket, null);                 
  }
  </code></pre>
  
  <p>addClientSocket is the function that because of it i'm getting the error.</p>