我想做一个类别树,这里是类别结构
class category{
private String id;
private String parentId;
private List<CategoryDto> children;
}
开头的children是空的,如果null是parent,则parentId是parent,否则是children。我想返回这个结果:
[
{
"id": "61eb379e6d797574df47cfc3",
"parentId": "",
"children": [
{
"id": "61ea7e5f1323dd3731dd304a",
"parentId": "61eb379e6d797574df47cfc3",
"children": null
},
{
"id": "61ea8f471323dd3731dd304c",
"status": "PUBLISHED",
"parentId": "61eb379e6d797574df47cfc3",
"children": null
}
]
}
]
我用经典编程做了我想用兵变 smallrye 迁移到反应式编程这是我的代码:
@Blocking
public Uni<List<CategoryDto>> getAllCategories(Request doc) {
log.info("REST request to find Categories : {}", doc);
Map<String, Object> parent = new HashMap<>();
List<CategoryDto> all = categoryRepository
.streamAllAggregators(doc).subscribe().asStream().map(it -> aggregatorMapper.categoryToCategoryDto(it, doc.getLang())).collect(toList());
List<CategoryDto> listParent = categoryRepository
.findByParentId("").subscribe().asStream().map(it -> aggregatorMapper.categoryToCategoryDto(it, doc.getLang())).collect(toList());
List<CategoryDto> listChildren = all.stream()
.filter(n -> n.getParentId() != null)
.collect(Collectors.toList());
List<CategoryDto> categoryDtoList = new ArrayList<>();
for (int i = 0; i < listParent.size(); i++) {
categoryDtoList = new ArrayList<>();
for (int j = 0; j < listChildren.size(); j++) {
if (listChildren.get(j).getParentId().equals(listParent.get(i).getId().toString())) {
categoryDtoList.add(listChildren.get(j));
}
}
listParent.get(i).setChildren(categoryDtoList);
}
return Uni.createFrom().item(listParent);
}