1
FamilyLegacy
   ParentList<Parent>

Parent //(Obj Class)
   ParentId // String ID
   ChildList<Child>  // List

Child
   ChildId
   GrandChildList<GrandChild>

GrandChild
    GrandChildId
   // Some GrandChild attribs    



{
  "parents": [
    {
      "Id": "P1",
      "parentName": "BigBob",
      "Child": [
        {
          "id": "C1",
          "name": "BOB",
          "grandChild": [
            {
              "grandChildId": "G1",
              "grandChildName": "John"
            },
            {
              "grandChildId": "G2",
              "grandChildName": "Doe"
            }
          ]
        },
        {
          "id": "C2",
          "name": "Marley",
          "grandChild": [
            {
              "grandChildId": "G3",
              "grandChildName": "Katty"
            },
            {
              "grandChildId": "G4",
              "grandChildName": "Perry"
            }
          ]
        }
      ]
    }
  ]
}

Outputs to ->

{
  "P1": {
    "name": "BigBob",
    "C1": {
      "name": "Bob",
      "G1": {
        "name": "John"
      },
      "G2": {
        "name": "Doe"
      }
    },
    "C2": {
      "name": "Marey",
      "G3": {
        "name": "Katty"
      },
      "G4": {
        "name": "Parry"
      }
    }
  }
}

Consider the above nested object to list structure. Is there a way to convert this structure to a nested Map such as

 Map<String,Map<String, Map<String,Map<String,GrandChild>>>>
ParentId -> map of Parents ->map of childs ->map of grand childs  keyed with respective IDs

For instance First Level Mapping

  Map<String, Parent> parentMap =
    FamilyLegacy.getParents().stream().collect(
                   Collectors.toMap(Parent::getParentId,Function.identity()));

I am having a hard time visualizing the mapping and nesting, would appreciate if there is a way to do it using direct grouping or map other than iterating individual list objects

3
  • Can you provide a reproducible example? Commented Jan 31, 2020 at 5:16
  • added a Json example to visualize the data structure. Commented Jan 31, 2020 at 5:43
  • Seriously, that data-structure is way too complex. Think about creating an utility class. A NestedMap4<K1, K2, K3, K4, V> maybe. Or, since this is about JSON, consider creating POJOs that match your structure and then read it in with GSON for example. Commented Jan 31, 2020 at 6:12

1 Answer 1

2

You can use Collectors.groupingBy to compose a nested map:

children.stream().collect(
    Collectors.groupingBy(
        gc -> gc.getChild().getParent().getFamilyLegacy().getId(),
        Collectors.groupingBy(
            gc -> gc.getChild().getParent().getId(),
            Collectors.groupingBy(
                gc -> gc.getChild().getId()))));

You can see where does the nesting come from by lambdas for getting keys.

The type will be then Map<String, Map<String, Map<String, GrandChild>>>. The first key fill be of FamilyLegacy, second of Parent, third from Child.

Edit:

Starting from FamilyLegacy you can use Collectors.toMap, like this:

familyLegacy.getParents().stream().collect(
    Collectors.toMap(
        p -> p.getId(),
        p -> p.getChildList().stream().collect(
            Collectors.toMap(
                c -> c.getId(),
                c -> c.getGrandChildList().stream().collect(
                    Collectors.toMap(
                        gc -> gc.getId(),
                        Function.identity()))))));
Sign up to request clarification or add additional context in comments.

2 Comments

THanks but looks like the flow is reverse there is no dependency of child in grand child and so on. Its the other way round. Let me put a JSON document that I am trying to visualize with some data.
thankyou.. that was exactly what i was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.