我有HashMap
个叫testMap
,包含String, String
个.
HashMap<String, String> testMap = new HashMap<String, String>();
在迭代映射时,如果value
与指定的字符串匹配,我需要从映射中删除键.
即
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)"