title: Can I flatten multi depth self-referencing entities?
body: <p>I have an entity self-references.</p>
  
  <pre><code>public class Category {
  
      private List&lt;Category&gt; children;
  }
  </code></pre>
  
  <p>With given list of Categories, how can I flatten them all?</p>
  
  <pre><code>C11
      C21
          C31
          C32
      C22
  C12
  </code></pre>
  
  <p>I want a list of</p>
  
  <pre><code>C1 C21 C31 C32 C22 C12
  </code></pre>
  
  <hr>
  
  <p>I tried.</p>
  
  <pre class="lang-java prettyprint-override"><code>public static &lt;T extends BaseEntity &amp; SelfReferencing&lt;T&gt;&gt; void flatten(
          final T parent, final Function&lt;T, Stream&lt;T&gt;&gt; function,
          final Consumer&lt;T&gt; consumer) {
      function.apply(parent).forEach(child -&gt; {
          consumer.accept(child);
          flatten(child, function, consumer);
      });
  }
  </code></pre>