title: How can I get the list of all the items nested(hierarchy) within the item based on parent-child relationship?
body: <p>It's a little bit difficult to explain. In this example code:</p>
  
  <pre><code>public class SomeClass
  {
      private String id;
      private String parent;
  
      public SomeClass(String id, String parent)
      {
          this.id = id;
          this.parent = parent;
      }
  
      public String getParent()
      {
          return parent;
      }
  }
  
  List&lt;SomeClass&gt; someList = new ArrayList();
  someList.add(new SomeClass("Test1", "none"));
  someList.add(new SomeClass("Test2", "none"));
  someList.add(new SomeClass("Test1Mem1", "Test1"));
  someList.add(new SomeClass("Test2Mem1", "Test2"));
  someList.add(new SomeClass("Test1Mem1Obj1", "Test1Mem1"));
  </code></pre>
  
  <p>I want to create a function that will fetch all the objects that is containing an object in it's hierarchy with the "parent" field. So for example if I look up for "Test1Mem1Obj1", it should give me the values of "{Test1Mem1, Test1}" and if I look up for "Test2Mem1", it should give me the values of "{Test2}". Basically fetches the parent of the parent of the parent and so on. I am sorry for this explanation because of the language barrier. I hope someone can help me out here. Thank you!</p>
  
  <p>I have a temporary dirty solution and you can see why this is not good.</p>
  
  <pre><code>if(someObj.getParent() != null)
  {   
      result.add(someObj.getParent());
  
      if(someObj.getParent().getParent() != null)
      {
          result.add(someObj.getParent().getParent());
  
          if(someObj.getParent().getParent().getParent() != null)
          {
              result.add(someObj.getParent().getParent().getParent());
          }
      }
  }
  </code></pre>