我可以帮您进行翻译。以下是您需要翻译的内容:
上述地图将包含父项作为键和映射到父项的值的地图。
我的输出结果如下:
请查看并告诉我我们如何实现相同的内容。先行致谢。
我需要根据父子关系将数据列表转换为列表。如果父级为空,则降到一级,二级将基于一级 id。
我的数据如下所示:
[
{id:1, parent: null },
{id:2, parent: 1 },
{id:3, parent: 1 },
{id:4, parent: 1 },
{id:5, parent: 2 },
{id:6, parent: 2 },
{id:7, parent: 3 },
{id:8, parent: 3 },
{id:9, parent: 4 },
{id:10, parent: 4 },
{id:11, parent: 5 },
{id:12, parent: null },
{id:13, parent: 12 },
]
我的代码是:
响应数据
Map<String,Map<String,ResponseData>> map = new HashMap<>();
for (ResponseData responseData : responseDataList) {
Map<String,responseData> responseDatasMap = map.get(responseData.getParent());
if(responseDatasMap != null) {
responseDatasMap.put(responseData.getId(),responseData);
map.put(responseData.getParent(),responseDatasMap);
} else {
responseDatasMap = new HashMap<>();
responseDatasMap.put(responseData.getParent(),responseData);
map.put(responseData.getParent(),responseDatasMap);
}
}
上述地图将包含父项作为键和映射到父项的值的地图。
List<List<ResponseData>> sections = new ArrayList<>();
for (Map.Entry<String,Map<String, ResponseData>> responseDataMap : map.entrySet()) {
Map<String, ResponseData> valueMap = responseDataMap.getValue();
responseDataList = new ArrayList<>();
for(Map.Entry<String, ResponseData> responseData :valueMap.entrySet()) {
responseDataList.add(responseData.getValue());
}
sections.add(responseDataList);
}
我的输出结果如下:
[
[ {id:1, parent: null } ],
[ {id:2, parent: 1 },{id:3, parent: 1 },{id:4, parent: 1 } ],
[ {id:5, parent: 2 },{id:6, parent: 2 } ],
[ {id:7, parent: 3 },{id:8, parent: 3 } ],
[ {id:9, parent: 4 },{id:10, parent: 4 } ],
[ {id:11, parent: 5 }]
]
但是我期望的输出应该像下面这样:
First level array contains the data where parent is null
Second level contains the children of level 1 ie id 2,3,4 are a child of id 1
Third level contains the children of levlel2 objects ie 5 & 6 are a child of 2 , 7 & 8 are a child of 3 and 9 & 10 child of 4
Level four contains the children of level 3 objects ie 11 is a child of id 5
[
[ {id:1, parent: null }, {id:12, parent:null} ],
[ {id:2, parent: 1 },{id:3, parent: 1 },{id:4, parent: 1 },{id:13, parent: 12 } ],
[ {id:5, parent: 2 },{id:6, parent: 2 },{id:7, parent: 3 },{id:8, parent: 3 },{id:9, parent: 4 },{id:10, parent: 4 } ],
[ {id:11, parent: 5 }]
]
请查看并告诉我我们如何实现相同的内容。先行致谢。