title: Display ArrayList data in tree structure in java
body: <p>I have an arrayList with following values:</p>
  
  <pre><code>static ArrayList&lt;DTONodeDetail&gt; tree;
  public static void main(String[] args) {
      // TODO Auto-generated method stub
      tree=new ArrayList&lt;DTONodeDetail&gt;();
  
       //first argument-&gt;NodeId
       //second-&gt;NodeName
       // third -&gt; ParentNodeId
  
      tree.add(getDTO(1,"Root",0));
      tree.add(getDTO(239,"Node-1",1));
      tree.add(getDTO(242,"Node-2",239));
      tree.add(getDTO(243,"Node-3",239));
      tree.add(getDTO(244,"Node-4",242));
      tree.add(getDTO(245,"Node-5",243));
      displayTree(tree.get(0));       
  
  }
  
  public static DTONodeDetail getDTO(int nodeId,String nodeName,int parentID)
  {
      DTONodeDetail dto=new DTONodeDetail();
      dto.setNodeId(nodeId);
      dto.setNodeDisplayName(nodeName);
      dto.setParentID(parentID);
  
      return dto;
  }
  </code></pre>
  
  <p><strong>Now i want to display above data in tree structure as below using simple java code:</strong></p>
  
  <pre><code>Root
  -----Node-1
  ------------Node-2
  ------------------Node-4
  ------------Node-3
  ------------------Node-5
  </code></pre>
  
  <p>I have tried following but unable to get desire result:</p>
  
  <pre><code>public static void displayTree(DTONodeDetail dto){
  
      ArrayList&lt;DTONodeDetail&gt; childs = selectChild(dto.getNodeId());
      System.out.println(dto.getNodeDisplayName());
      for(DTONodeDetail obj:childs){
  
          displayTree(obj);
      }
  
  }
  
  public static ArrayList&lt;DTOWorkSpaceNodeDetail&gt;  selectChild(int nodeID){
          ArrayList&lt;DTOWorkSpaceNodeDetail&gt; list=new ArrayList&lt;DTOWorkSpaceNodeDetail&gt;();
  
          for(int i=0;i&lt;tree.size();i++)
          {
              if(tree.get(i).getParentID()==nodeID){
  
                  list.add(tree.get(i));
  
              }       
          }
          return list;
  
      }
  </code></pre>
  
  <p>Please Provide some guide or code.</p>