-1

I am using iterator to delete item from linked hash map but getting concurrent modification exception

Iterator<Integer> it = linkedMap.keySet().iterator();

    while (it.hasNext()) {
        java.lang.Integer key = it.next();
        if (key.equals(number)) {
            linkedMap.remove(key);
        }
    }

Please Help..

1 Answers1

1

You need to remove iterator when check for condition not the linkedMAp

 Iterator<Integer> it = linkedMap.keySet().iterator();
    while (it.hasNext()) {
     Integer key = it.next();
        if (key.equals(number)) {
           // Remove the current element from the iterator and the list.
            it.remove();
        }
    }

check also this question is just like yours

Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop

Community
  • 1
  • 1
Kristiyan Varbanov
  • 2,439
  • 2
  • 17
  • 37