我是Java的初学者,有人可以帮我解释一下原因以及如何解决它。谢谢大家。
public class MainMap {
public static void main(String[] args) {
HashMap<String, Object> map
= new HashMap<>();
String[] keys = new String[]{"hobby","address"};
map.put("name", "Q10Viking");
map.put("hobby", "football");
map.put("address", "street-01");
map.put("score",new BigDecimal(30.9));
map.forEach((key,val)->{
if(Arrays.stream(keys).anyMatch(key::equals)){
map.remove(key);
}
});
System.out.println(map);
}
}
错误
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap.forEach(HashMap.java:1292)
at org.caucoder.formap.MainMap.main(MainMap.java:21)
“我期望输出:”
{name=Q10Viking, score=30.9}