title: Primefaces dynamic tree
body: <p>I am developing a Tree component, which might be prunned according to the user input. I have all nodes cached, in order to provide primefaces component model with always the same instances of the tree nodes.</p>
  
  <p>But the problem is, that when I change the parent-child relationships (remove some of them), I always get a concurrent modification exception... Is there any way to workaround this issue or am I missing something.</p>
  
  <p>Thanks in advance.</p>
  
  <pre><code>/**
   * Recursively copy the hierarchy and prune not visible branches
   * @param originalNode node of the original hierarchy to be copied
   * @param parent parent node in the new hierarchy
   * @param visibleNodes set of nodes, which should be included in the new hierarchy
   * @param model model of the TreeAutompleteComponent
   * @return copy of the oroginal node in the new hierarchy
   */
  private TreeNode copyHierarchy(TreeNode originalNode, TreeNode parent, Set&lt;TreeNode&gt; visibleNodes, TreeAutocompleteModel model) {
      if (!visibleNodes.contains(originalNode)) {
          return null;
      }
  
      TreeNode newNode = model.getCompleteModelNodes().get(((NodeWrapper)originalNode.getData()).getIri());//new DefaultTreeNode(originalNode.getData(), parent);
      for (int i = 0; i &lt; originalNode.getChildCount(); i++) {
          copyHierarchy((TreeNode) originalNode.getChildren().get(i), newNode, visibleNodes, model);
      }
      newNode.setParent(parent); //when removed, no exception occurs
      return newNode;
  }
  </code></pre>
  
  <p>The exception</p>
  
  <pre><code>ava.util.ConcurrentModificationException
      at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
      at java.util.ArrayList$Itr.next(ArrayList.java:791)
      at org.primefaces.component.tree.TreeRenderer.encodeTreeNodeChildren(TreeRenderer.java:284)
      at org.primefaces.component.tree.TreeRenderer.encodeTreeNode(TreeRenderer.java:274)
      at org.primefaces.component.tree.TreeRenderer.encodeMarkup(TreeRenderer.java:165)
      at org.primefaces.component.tree.TreeRenderer.encodeEnd(TreeRenderer.java:96)
      at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
      at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
      at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
      at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
      at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
      at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
      at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
      at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
      at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
      at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
      at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
      at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
      at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
      at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
      at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
      at com.sun.faces.renderkit.html_basic.CompositeRenderer.encodeChildren(CompositeRenderer.java:78)
      at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
      at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
      at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
      at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:424)
      at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125)
      at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:182)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
      at java.lang.Thread.run(Thread.java:722)
  </code></pre>