title: Categories - sub categories traversal algorithm
body: <p>I am looking for good way to traverse a list of categories to create an hierarchy. The present algorithm I am using will not be able to take deep nesting into consideration.</p>
  
  <p>I have a list of all categories (including parent and their all sub-categories), category class is like below:</p>
  
  <pre><code>class Category{
       private int id;
       private int parentCategoryId;
       private String name;
  
       /*Getter Setters*/
  }
  </code></pre>
  
  <p>Here the <code>parentCategoryId</code> stores the <code>id</code> of its parent, root categories will be having <code>parentCategoryId = 0</code> and their could be many root categories. Current condition requires me to extend the nesting to atleast 5-6 levels deep, and my current algo fails over that.</p>
  
  <p>What could be a good way to arrange them in such an order that I can easily iterate over them in my view to build something like this:</p>
  
  <pre>
  -Root1
  --Root1Sub1
  --Root1Sub2
  ---Root1Sub2SubSub1
  ---Root1Sub2SubSub2
  -Root2
  --Root2Sub1
  </pre>