Search code examples
javahashmapconcurrentmodification

ConcurrentModificationException while updating values


The update function should update each value in the HashMap by calling the function test to determine the new value. The test function returns either a 1 or 0 depending on the 8 locations/neighbors around it. Although, I get a ConcurrentModificationException every time the program gets to the update function.

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()<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()<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()<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()<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()<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()<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()<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()<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 < 2){
            temp = 0;
        } else if(sum == 2 || sum == 3){
            temp = 1;
        } else if(sum > 3){
            temp = 0;
        }
    } else {
        if(sum == 3){
            temp = 1;
        }
    }
    return temp;
}

Solution

  • You are getting ConcurrentModificationException because you are modifying the Map while you are iterating over it, which is not allowed.

    You can make a copy of your current map for iteration. Instead of doing operations on the same map, iterate over one and do manipulation over the second, then use the second map as output.

    Read about ConcurrentModificationException for more understanding.