I am having HashMap
called testMap
which contains String, String
.
HashMap<String, String> testMap = new HashMap<String, String>();
When iterating the map, if value
is match with specified string, I need to remove the key from map.
i.e.
for(Map.Entry<String, String> entry : testMap.entrySet()) {
if(entry.getValue().equalsIgnoreCase("Sample")) {
testMap.remove(entry.getKey());
}
}
testMap
contains "Sample"
but I am unable to remove the key from HashMap
.
Instead getting error :
"Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(Unknown Source)
at java.util.HashMap$EntryIterator.next(Unknown Source)
at java.util.HashMap$EntryIterator.next(Unknown Source)"