I have list hierarchy object, which can have list of grouping object each having single parent and multiple child values to parent_value object.Input from java object looks like below:
<hierarchy>
<grouping>
<parent_value>
<value>3825709035</value>
<display>Car</display>
</parent_value>
<child_value>
<value>3825709039</value>
<display>Baleno</display>
</child_value>
<child_value>
<value>3825709051</value>
<display>I20</display>
</child_value>
</grouping>
<grouping>
<parent_value>
<value>3825709011</value>
<display>Vehicle</display>
</parent_value>
<child_value>
<value>3825709027</value>
<display>Bus</display>
</child_value>
<child_value>
<value>3825709015</value>
<display>Bike</display>
</child_value>
<child_value>
<value>3825709015</value>
<display>Car</display>
</child_value>
</grouping>
</hierarchy>
I want output something like this, after iterating first grouping list, I want Car parent having Baleno and I20 if that was the only object in that list.
Car
Baleno
I20
but after reading second grouping, since Vehicle also has Car as child, child should not come as separate section instead come under Vehicle parent. My final output should not come like this
Car
Baleno
I20
Vehicle
Bus
Bike
Car
Baleno
I20
instead final output should be like below:
Vehicle
Bus
Bike
Car
Baleno
I20
How to we come up with logic in java where we can iterate each grouping and have track of each child and its parent so that if we have parent as child of another parent then it should come in child section and not as parent assuming we have
List groups = hierarchy.getGroups();
and Grouping object has common object CommonHelper since same value and display exists for both parent_value and child_value.Grouping class will have
class Grouping
{
CommonHelper parent_value;
List<CommonHelper> child_value;
}
class CommonHelper
{
long value;
String display;
}