遍历集合,若同时对集合元素进行操作时(如删除等),要使用Iterator迭代器
public List<Map<String,Object>> deleList(List<Map<String,Object>> materielMainList,String pinhao){
Iterator<Map<String, Object>> iterator = materielMainList.iterator();
while (iterator.hasNext()) {
Map<String,Object> temp = iterator.next();
String ph = temp.get("PINGHAO")+"";
if(pinhao.equals(ph)){
iterator.remove();
}
}
/*错误代码
for(Map<String,Object> tempmap : materielMainList){
String ph = tempmap.get("PINGHAO")+"";
if(pinhao.equals(ph)){
materielMainList.remove(tempmap);
}
}*/
return materielMainList;
}
报错信息:
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at com.ekingwin.bas.rest.service.MaterielMainService.deleList(MaterielMainService.java:188)