Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7741769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:06:44+00:00 2026-06-01T09:06:44+00:00

I get ConcurrentModificationException in the following piece of code when i running the code

  • 0

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 .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:06:45+00:00Added an answer on June 1, 2026 at 9:06 am

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I execute the following code, I get ConcurrentModificationException Collection<String> myCollection = Collections.synchronizedList(new ArrayList<String>(10));
Here is my problem: This piece of code throws a java.util.ConcurrentModificationException , because the
I have the following piece of code: private String toString(List<DrugStrength> aDrugStrengthList) { StringBuilder str
I'm getting the following errors: Exception in thread main java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(HashMap.java:810) at java.util.HashMap$KeyIterator.next(HashMap.java:845)
On this code I get an java.util.ConcurrentModificationException the method is in a webservice and
my code throw follow exception: java.util.ConcurrentModificationException at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:761) at java.util.LinkedList$ListItr.next(LinkedList.java:696) at java.util.AbstractCollection.addAll(AbstractCollection.java:305) at java.util.LinkedHashSet.<init>(LinkedHashSet.java:152)
Get an empty HTML, type in this and see its source code on Google
We all know you can't do the following because of ConcurrentModificationException : for (Object
I get a ConcurrentModificationException even though I made all the methods of the whole
//get the lat and loni throgh the loaddata function.java.util.ConcurrentModificationException at java.util.AbstractList$SimpleListIterator.next(AbstractList.java:64) How to resolve

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.