title: Iterate through collection in collection until last element in Java
body: <p>I have this class:</p>
  
  <pre><code>public class SimpleEntity {
      private String id;
      private String name;
      private boolean closed;
      private List&lt;SimpleEntity &gt; simpleEntityChildren;
  }
  </code></pre>
  
  <p>I want to iterate recursively into <code>SimpleEntityChildren</code> until the last element and I don't know how to implement it.</p>
  
  <pre><code>public void iterator(SimpleEntity simpleEntity ){
      simpleEntity .forEach(entity-&gt; {
          if(!entity.getSimpleEntityChildren.isEmpty()){
              entity.forEach(secondEntity-&gt; {
                  if(!secondEntity.getSimpleEntityChildren.isEmpty()){
                      //and so on to the last element
                  }
              });
          }
      });
  }
  </code></pre>