title: ConcurrentModificationException while updating values
body: <p>The <code>update</code> function should update each value in the <code>HashMap</code> by calling the function <code>test</code> to determine the new value. The <code>test</code> function returns either a 1 or 0 depending on the 8 locations/neighbors around it. Although, I get a <code>ConcurrentModificationException</code> every time the program gets to the update function. </p>
  
  <pre><code>private static void update(){
     for(Cell e : map.keySet()){
         map.put(e,test(e.getX(),e.getY()));
    }
  }
  
  private static int test(int i, int j){
      //count alive neighbors
      int sum = 0;
      if(map.get(new Cell(i-1, j - 1)) == null){
          map.put(new Cell(i-1, j - 1), ((Math.random()&lt;0.5)?0:1));
          sum += map.get(new Cell(i-1, j - 1));
      } else {
          sum += map.get(new Cell(i-1, j - 1));
      }
  
      if(map.get(new Cell(i, j - 1)) == null){
          map.put(new Cell(i, j - 1), ((Math.random()&lt;0.5)?0:1));
          sum += map.get(new Cell(i, j - 1));
      } else {
          sum += map.get(new Cell(i, j - 1));
      }
  
      if(map.get(new Cell(i + 1, j - 1)) == null){
          map.put(new Cell(i + 1, j - 1), ((Math.random()&lt;0.5)?0:1));
          sum += map.get(new Cell(i + 1, j - 1));
      } else {
          sum += map.get(new Cell(i + 1, j - 1));
      }
  
      if(map.get(new Cell(i + 1, j)) == null){
          map.put(new Cell(i + 1, j), ((Math.random()&lt;0.5)?0:1));
          sum += map.get(new Cell(i + 1, j));
      } else {
          sum += map.get(new Cell(i + 1, j));
      }
  
      if(map.get(new Cell(i + 1, j + 1)) == null){
          map.put(new Cell(i + 1, j + 1), ((Math.random()&lt;0.5)?0:1));
          sum += map.get(new Cell(i + 1, j + 1));
      } else {
          sum += map.get(new Cell(i + 1, j + 1));
      }
  
      if(map.get(new Cell(i, j + 1)) == null){
          map.put(new Cell(i, j + 1), ((Math.random()&lt;0.5)?0:1));
          sum += map.get(new Cell(i, j + 1));
      } else {
          sum += map.get(new Cell(i, j + 1));
      }
  
      if(map.get(new Cell(i - 1, j + 1)) == null){
          map.put(new Cell(i - 1, j + 1), ((Math.random()&lt;0.5)?0:1));
          sum += map.get(new Cell(i - 1, j + 1));
      } else {
          sum += map.get(new Cell(i - 1, j + 1));
      }
  
      if(map.get(new Cell(i - 1, j)) == null){
          map.put(new Cell(i - 1, j), ((Math.random()&lt;0.5)?0:1));
          sum += map.get(new Cell(i - 1, j));
      } else {
          sum += map.get(new Cell(i - 1, j));
      }
  
      //return to be alive or dead
      int temp = 0;
      if(map.get(new Cell(i,j)) == 1){
          if(sum &lt; 2){
              temp = 0;
          } else if(sum == 2 || sum == 3){
              temp = 1;
          } else if(sum &gt; 3){
              temp = 0;
          }
      } else {
          if(sum == 3){
              temp = 1;
          }
      }
      return temp;
  }
  </code></pre>