迭代器中的ConcurrentModificationException帮助 [英] ConcurrentModificationException help in iterator

查看:161
本文介绍了迭代器中的ConcurrentModificationException帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码片段中获得ConcurrentModificationException



当我运行代码,它很好,但突然它抛出一个异常,我猜它是由于修改列表,但我不知道如何修复它

  if(myRulesIncr!= null)
{
Iterator itReceivedRules = myRulesIncr.iterator();
while(itReceivedRules.hasNext())
{
RuleModel currentReceived =(RuleModel)itReceivedRules.next();
if(receivedRulesExisting!= null)
{
Iterator itReceivedRulesExisting = receivedRulesExisting.iterator();
while(itReceivedRulesExisting.hasNext())
{
RuleModel currentExisting =(RuleModel)itReceivedRulesExisting.next();

if(currentExisting.getRuleId()。equals(currentReceived.getRuleId()))
{
// TODO:替换规则,否则添加它。
if(currentReceived.getStatus()!=D)
{
//用新的规则替换现有规则
receivedRulesExisting.remove(currentExisting);
receivedRulesExisting.add(currentReceived);
}
else
{
receivedRulesExisting.remove(currentExisting);
}
}
else
{
//将新规则添加到现有规则
receivedRulesExisting.add(currentReceived);
}
}
}
}
}


$ b

当被迭代的集合被外部修改时抛出,即不通过迭代器。因此,您需要使用 Iterator.remove() 以避免此异常。此外,不是直接在迭代时直接添加到集合中,而是将要添加的项目存储在单独的集合中,然后再添加它们:

  List< RuleModel> toBeAdded = new ArrayList< RuleModel>(); 

if(currentReceived.getStatus()!=D)
{
//用新的规则替换现有规则
itReceivedRulesExisting.remove
toBeAdded.add(currentReceived);
}
else
{
itReceivedRulesExisting.remove();
}

...
//循环结束后:
receivedRulesExisting.addAll(toBeAdded);

请注意,我使用了通用集合 - 建议这样做,如下所示:

  Collection< RuleModel> myRulesIncr = ... 
...
迭代器< RuleModel> itReceivedRules = myRulesIncr.iterator();
...
RuleModel currentReceived = itReceivedRules.next();


I get ConcurrentModificationException in the following piece of code

when i running the code it went fine but all of a sudden it throws a exception, i guess its due to the modification of the list, but i'm not sure how to fix it

if (myRulesIncr!=null)
{
    Iterator itReceivedRules = myRulesIncr.iterator();
    while (itReceivedRules.hasNext())
    {
      RuleModel currentReceived = (RuleModel) itReceivedRules.next();
      if (receivedRulesExisting!=null)
      {
        Iterator itReceivedRulesExisting = receivedRulesExisting.iterator();
        while (itReceivedRulesExisting.hasNext())
        {
            RuleModel currentExisting = (RuleModel) itReceivedRulesExisting.next();

            if(currentExisting.getRuleId().equals(currentReceived.getRuleId()))
            {
                //TODO:replace the rule else add it.
                if(currentReceived.getStatus()!="D")
                { 
                    //replace the existing rule with the new one
                    receivedRulesExisting.remove(currentExisting);
                    receivedRulesExisting.add(currentReceived);
                } 
                else
                {
                    receivedRulesExisting.remove(currentExisting); 
                }
            }
            else
            {
                //Add the new rule to the existing rules
                receivedRulesExisting.add(currentReceived);
            }
        }
      }
    }
}

Please help me out in this .

解决方案

ConcurrentModificationException is thrown when the collection being iterated is modified externally, i.e. not via the iterator. So you need to use Iterator.remove() to avoid this exception. Moreover, instead of adding directly to the collection while iterating through it, store the items to be added in a separate collection, then add them afterwards:

  List<RuleModel> toBeAdded = new ArrayList<RuleModel>();

  if(currentReceived.getStatus()!="D")
  { 
      //replace the existing rule with the new one
      itReceivedRulesExisting.remove();
      toBeAdded.add(currentReceived);
  } 
  else
  {
      itReceivedRulesExisting.remove(); 
  }

  ...
  // after the loop terminated:
  receivedRulesExisting.addAll(toBeAdded);

Note also that I used a generic collection - it is advisable to do so, to ensure type safety and get rid of downcasts like so:

Collection<RuleModel> myRulesIncr = ...
...
Iterator<RuleModel> itReceivedRules = myRulesIncr.iterator();
...
RuleModel currentReceived = itReceivedRules.next();

这篇关于迭代器中的ConcurrentModificationException帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆