title: Generate "explorer tree" in Java efficiently (not using recursion)
body: <p>I have a question which could be language-agnostic but for this particular implementation I'm using Java. It is possible and relatively trivial to list the folders in a directory - using a function like this:</p>
  
  <pre><code>private DefaultMutableTreeNode GenerateFSTree(File f)
  {
      int i = 0;
      File[] Children = f.listFiles();
  
      DefaultMutableTreeNode x = new DefaultMutableTreeNode(f.getName());
  
      if ( Children != null )
      {
          for ( i = 0; i &lt; Children.length; i++ )
          {
              File f_cur = Children[i];
              if (
              f_cur.isDirectory() &amp;&amp; 
              ( this.DisplayHidden || !f_cur.isHidden() ) 
              )
              {   
                  x.add(GenerateFSTree(f_cur));
              }
          }
      }
  
      return x;
  }
  </code></pre>
  
  <p>As you can see this makes heavy use of recursion to evaluate the filesystem and what you end up with is a tree of <code>DefaultMutableTreeNode</code> items.</p>
  
  <p>Now my question is - is there a faster way to do this? There must be, because this is slow. Try executing this on <code>/</code> and it takes forever. Yet if I use say Nautilus or the built-in Java File selection dialog, the tree renders instantly.</p>
  
  <p>So my question is - how can I speed this up?</p>
  
  <p>Thanks</p>