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
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.