I'm just starting with recursion, so if someone can try to help I will very much appreciate it.
I have the following `String`:
Note that I can have groups inside groups (subgroups). I can also have groups inside groups inside groups, so recursion is mandatory as far as I know.field1: typeA field2: typeA field3: typeB group1: fieldA: typeA fieldB: typeB subGroup1: fieldX: typeB fieldY: typeB fieldC: typeC field4: typeA group2: fieldA: typeB subGroup1: fieldX: typeA fieldY: typeA fieldB: typeA fieldC: typeC field5: typeC field6: typeD
Note that the *spacing* in the beginning of each line determines if the field belongs to the group or not.
I would like to write a method in Java that will return a `LinkedHashMap<String, Object>`, where the *Object* map value can either be a `String` denoting that this is a field (not a group) or another `LinkedHashMap<String, Object>` denoting that this is a group (not a field).
I've tried but failed miserably. Recursion is just too hard for me at the moment, but I'm studying it.
To help whoever wants to help, below the code:
public static void main(String[] args) { String text = String.join("\n", "field1: typeA", "field2: typeA", "field3: typeB", "group1:", " fieldA: typeA", " fieldB: typeB", " subGroup1:", " fieldX: typeB", " fieldY: typeB", " fieldC: typeC", "field4: typeA", "group2:", " fieldA: typeB", " subGroup1:", " fieldX: typeA", " fieldY: typeA", " fieldB: typeA", " fieldC: typeC", "field5: typeC", "field6: typeD", ""); Map<String, Object> solution = doIt(text); System.out.println(solution); } private static LinkedHashMap<String, Object> doIt(String text) { // solution goes here... return null; } }