title: How to unflatten a flattened hierarchy with Streams API
body: <p>If I have a hierarchical structure that is still sorted, but flattened - how can I create a parent/child structure with Java Streams API? An example:
  How do I go from</p>
  
  <pre><code>-,A,Foo
  A,A1,Alpha1
  A,A2,Alpha2
  -,B,Bar
  B,B1,Bravo1
  B,B2,Bravo2
  </code></pre>
  
  <p>to</p>
  
  <pre><code>-
    A
      A1,Alpha1
      A2,Alpha2
    B
      B1,Bravo1
      B2,Bravo2
  </code></pre>
  
  <p>A simple non-stream way would be to keep track of the parent column and see if it has changed.</p>
  
  <p>I have tried various ways with Collectors and groupingBy, but have not yet found how to do.</p>
  
  <pre><code>List&lt;Row&gt; list = new ArrayList&lt;&gt;();
  list.add(new Row("-", "A", "Root"));
  list.add(new Row("A", "A1", "Alpha 1"));
  list.add(new Row("A", "A2", "Alpha 2"));
  list.add(new Row("-", "B", "Root"));
  list.add(new Row("B", "B1", "Bravo 1"));
  list.add(new Row("B", "B2", "Bravo 2"));
  
  //Edit
  Map&lt;Row, List&lt;Row&gt;&gt; tree;
  
  tree = list.stream().collect(Collectors.groupingBy(???))
  </code></pre>