Solved

ConcurrentModificationException while using addAll method

Posted on 2009-05-13
13
358 Views
Last Modified: 2012-05-06
                 these loop will read the values from the form and put it in requestList.
                  
                  for (int i = 1; i <= 1; i++) {
                         cdtls= new ContainerDetails();
                         cdtls.setContainerType(request.getParameter("ContainerType_"+i+"_1"));
                         cdtls.setContainerNumber(request.getParameter("ContainerNumber_"+i+"_2"));
                         cdtls.setContainerDesc(request.getParameter("ContainerDescription_"+i+"_3"));
                         AL.add(cdtls);
                  }

                         ArrayList requestList=al;

                        //this list will give me first 10 records for the particular booking id;

                         ArrayList dataBaseList =DAO.getDBDetails(bookingId);

                  
                  ArrayList addList= new ArrayList();
                  addList.add("10");

                  // compared the addList number versus the dataBaseList ContainerDetails object id, if both r
                  same then first i need to remove that id from dataBaseList and then add requestList to dataBaseList.
                  i am getting java.util.ConcurrentModificationException while doing addAll method.
                  whats wrong with this code?

                  if(addList != null && !addList.get(0).equals("") && addList.size() > 0)
                        {
                              for (Iterator iterator = addList.iterator(); iterator.hasNext();) {
                                    String addId = (String) iterator.next();
                                    
                                    for (Iterator iterator2 = dataBaseList.iterator(); iterator2
                                                .hasNext();) {
                                          ContainerDetails object = (ContainerDetails) iterator2.next();
                                    
                                          if(object.getId() == Integer.parseInt(addId))
                                          {
                                                iterator2.remove();
                                                dataBaseList.addAll(al);
            
                                          }

                              }
                           }
                        }
0
Comment
Question by:chaitu chaitu
  • 4
  • 4
  • 2
  • +2
13 Comments
 
LVL 12

Expert Comment

by:Gibu George
ID: 24375143
Are you using Threads in the application
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 24375159
no
0
 
LVL 12

Expert Comment

by:Gibu George
ID: 24375191
What type of application is this is it a web application?
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 24375220
yes
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24375407
The first loop will only execute once, is that your intention?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 24375506
You can't addAll to dataBaseList while you're iterating it. Try instead:
        if(addList != null && !addList.get(0).equals("") && addList.size() > 0)

        {

            List temp = new ArrayList();

            for (Iterator iterator = addList.iterator(); iterator.hasNext();) {

                String addId = (String) iterator.next();
 

                for (Iterator iterator2 = dataBaseList.iterator(); iterator2

                        .hasNext();) {

                    ContainerDetails object = (ContainerDetails) iterator2.next();
 

                    if(object.getId() == Integer.parseInt(addId))

                    {   

                        iterator2.remove();

                        //dataBaseList.addAll(al);

                        temp.addAll(al);
 

                    }
 

                }   

            }   

            if (temp.size() > 0)

            {

                dataBaseList.addAll(temp);

            }

        }

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 24380229
why not use a Map (keyed on id), that way you would not even need to loop at all.
using the appropriate data structure can greatly simplify your code

0
 
LVL 5

Expert Comment

by:avya2k
ID: 24381903
As per specifications, you can not modify contents while iterating a collection
Instead collect all contents in temparory list and add it to main list after iteration
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 24382038
objects,

can you give me some hint what you are trying to tell?
0
 
LVL 92

Expert Comment

by:objects
ID: 24382046
for example if you had a map keyed on id you would not need to loop through every element looking for a particular id.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24383323
>>Instead collect all contents in temparory list and add it to main list after iteration

(which is what happens in the code i posted)

chaituu, what result do you get when you run that?
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 24392159
CEHJ,

ur solution is working.i thought i am doing some unwanted code thats why i am taking objects suggestion.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24412017
:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now