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
-,A,Foo
A,A1,Alpha1
A,A2,Alpha2
-,B,Bar
B,B1,Bravo1
B,B2,Bravo2
to
-
A
A1,Alpha1
A2,Alpha2
B
B1,Bravo1
B2,Bravo2
A simple non-stream way would be to keep track of the parent column and see if it has changed.
I have tried various ways with Collectors and groupingBy, but have not yet found how to do.
List<Row> list = new ArrayList<>();
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<Row, List<Row>> tree;
tree = list.stream().collect(Collectors.groupingBy(???))