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..
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