我有一个名为testMap
的HashMap
,其中包含String, String
。
HashMap<String, String> testMap = new HashMap<String, String>();
在迭代map时,如果value
与指定的字符串匹配,我需要从map中删除这个键。
例如:
for(Map.Entry<String, String> entry : testMap.entrySet()) {
if(entry.getValue().equalsIgnoreCase("Sample")) {
testMap.remove(entry.getKey());
}
}
testMap
包含 "Sample"
,但我无法从 HashMap
中删除该键。而是会出现错误:
"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)"