ITeye博客

ConcurrentModificationException 的解决办法

bask · 2008-08-01 11:07 · java基础 · Markdown

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 其他处理
	}
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。