-4

This is the place which I am going to remove my item,

myhieararchy hierarchyforDisplay = null;

    try {

        hierarchyforDisplay = (myhieararchy)hieararchybefore.clone();

    } catch (CloneNotSupportedException e) {

        e.printStackTrace();

    }

    for (Project project : projectList) {

    for (Program program : hierarchyforDisplay.getPrograms()) {

        for (Project rootproject : program.getProject()) {

            if(project.getId() != rootproject.getProjectId()){

                program.getProject().remove(rootproject);
            }
        }
    }
    }


    return hierarchyforDisplay;

But I am getting this

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.util.ConcurrentModificationException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

I can not imagine what is the reson because this is my first time I get this.. :(

Raedwald
  • 46,613
  • 43
  • 151
  • 237
CodeIntro
  • 191
  • 1
  • 1
  • 10

2 Answers2

1

An item cannot be removed from a collection while that collection is being iterated except by using the iterator.remove() method. To use this you will need to convert from the implicit iterator of the enhanced for loop to an explicit iterator

    for (Project rootproject : program.getProject()) { //<-- here the enhanced for loop iterates over the collection

        if(project.getId() != rootproject.getProjectId()){

            program.getProject().remove(rootproject); //<--- here you attempt to remove from that collection
        }
    }

Convert to an explicit iterator and use the .remove() method

    Iterator<Project> it=program.getProject().iterator();

    while(it.hasNext()){
        Project rootproject=it.next();

        if(project.getId() != rootproject.getProjectId()){

            it.remove(); //<--- iterator safe remove
            // iterator remove removes whatever .next() returned from the backing array (be aware; is implimented for most BUT NOT ALL collections, additionally for some collections creating a new collection can be more efficient


        }
    }
Richard Tingle
  • 16,906
  • 5
  • 52
  • 77
0
for (Project rootproject : program.getProject()) {

        if(project.getId() != rootproject.getProjectId()){

            program.getProject().remove(rootproject);
        }
}

In the above code you are invoking remove() method on the collection while iterating with for loop. It is causing the ConcurrentModificationException. You can not perform any structural changes while iterating with a for loop.

Use Iterator if you want to make any structural changes while iterating.

Iterator<Project> itr = program.getProject().iterator();
while(itr.hasNext()){
    Project rootProject = itr.next();

    if(project.getId() != rootproject.getProjectId()){

        itr.remove(rootproject);
    }
}
Vaibhav Raj
  • 2,214
  • 4
  • 23
  • 39