1.出现 java.util.ConcurrentModificationException 时的解决办法
//(1) 根据某个id直接删除全部(如果数据库中的多对多关系)
Set<Largess> largessSet = promotion.getLargess();
largessSet.clear();
//或者
for(Iterator it = largessSet.iterator();it.hasNext();) {
Largess largess = (Largess)it.next();
it.remove();//先移除
largessSet.remove(largess);
}
//(2) 根据某个条件删除 就是用Iterator
for(Iterator it = promotionList.iterator();it.hasNext();) {
Promotion promotion = (Promotion)it.next();
it.remove();//先移除
if(promotion.getEndTime().before(new Date())) {
xxxManager.remove(promotion);
//TODO 其他处理
}
}
本文介绍了在Java中遇到ConcurrentModificationException时的两种解决方法:一是通过清空或迭代删除集合中的元素;二是根据特定条件移除元素。适用于处理多对多关系的数据删除场景。
2172

被折叠的 条评论
为什么被折叠?



