title: java.util.ConcurrentModificationException arises
body: <p>May Be it can be a duplicate question, I'm sorry for that. But my problem is not solving.</p>
  
  <p>Below code gives me this exception (java.util.ConcurrentModificationException ) when execution reaches for 2nd time in foreach loop. Even i removed the arraylist object using iterator.</p>
  
  <pre><code>@RequestMapping("edit")
  public ModelAndView editItemToInvoice(HttpSession session,@RequestParam("itemname")String itemname){
  
      ArrayList&lt;InvoiceEntities&gt; show=(ArrayList&lt;InvoiceEntities&gt;)session.getAttribute("invoices");
  
      if(show==null){
          show=new ArrayList&lt;InvoiceEntities&gt;();
      }
  
      ArrayList&lt;InvoiceEntities&gt; edit=new ArrayList&lt;InvoiceEntities&gt;();
  
      for(InvoiceEntities itemnam:show){
          if(itemnam.getItemName().equals(itemname)){
              int index=show.indexOf(itemnam);
              edit.add(show.get(index));
  
              Iterator&lt;InvoiceEntities&gt; iter = show.iterator();
              while(iter.hasNext()){
                  InvoiceEntities getitem=iter.next();
                  if(getitem.getItemName().equals(itemname)){
                      iter.remove();
                      //break;
                  }
              }
  
          }
      }
  
      System.out.println(session.getAttribute("invoices"));
  
      ModelAndView model=new ModelAndView();
      session.setAttribute("invoices", show);
      model.addObject("editobj",edit);
      model.addObject("items",session.getAttribute("invoices"));
      model.setViewName("jsp/Invoice");
  
      return model;
  }
  </code></pre>
  
  <p>Exception is  java.util.ConcurrentModificationException</p>
  
  <pre><code> SEVERE: Servlet.service() for servlet [spring-dispatcher] in context with  
   path [/Invoice] threw exception [Request processing failed; nested 
   exception is java.util.ConcurrentModificationException] with root cause
  java.util.ConcurrentModificationException
  at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
  at java.util.ArrayList$Itr.next(ArrayList.java:851)
  at   mine.Controllers.InvoiceContorller.editItemToInvoice(InvoiceContorller.java:71)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:483)
  at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
  at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
  at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
  at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
  at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
  </code></pre>