集合替代 - ConcurrentModificationException [英] Collection Alternative - ConcurrentModificationException
问题描述
我迭代一个强制执行失败快速迭代器概念的JRE集合。问题是,如果对象满足条件,我需要删除对象的逻辑合作伙伴。因此防止伙伴也被处理。有人可以为此目的建议更好的收藏类型吗?
I'm iterating over a JRE Collection which enforces the fail-fast iterator concept. Problem is I need to remove an object's logical partner if the object meets a condition. Thus preventing the partner from also being processed. Can someone suggestion a better collection type for this purpose?
示例。
myCollection<BusinessObject>
for (BusinessObject anObject : myCollection)
{
if (someConditionIsTrue)
{
myCollection.remove(anObjectsPartner); // throws ConcurrentModificationException
}
}
b $ b
推荐答案
您要从列表中删除一个项目,并继续在同一个列表上迭代。您可以实现两步解决方案,其中在步骤1中收集要在临时集合中删除的项目,并在步骤2中识别后删除它们?
You want to remove an item from a list and continue to iterate on the same list. Can you implement a two-step solution where in step 1 you collect the items to be removed in an interim collection and in step 2 remove them after identifying them?
这篇关于集合替代 - ConcurrentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!