Java stream处理递归
更新完整代码
private void queryLevelCategory(List<KnowledgeCategory> categoryList){
List<KnowledgeCategory> knowledgeCategories = this.deepCategory("thetopuuid0000000000000000000000", categoryList);
String s = JsonUtil.toJson(knowledgeCategories);
System.out.println(s);
}
private List<KnowledgeCategory> deepCategory(String uuid,List<KnowledgeCategory> categoryList){
List<KnowledgeCategory> result = new ArrayList<>();
categoryList.forEach(x->{
if (uuid.equals(x.getParentUuid())) {
List<KnowledgeCategory> childRenList = this.deepCategory(x.getUuid(), categoryList);
x.setChildRen(childRenList);
result.add(x);
}
});
return result;
}
![文件上传中...]()
响应json数据
[
{
"uuid": "category000000000000000000000009",
"categoryName": "知识管理默认一级分类(2)",
"parentUuid": "thetopuuid0000000000000000000000",
"describeInfo": "知识管理默认一级分类",
"lever": 1,
"childRen": [
{
"uuid": "category000000000000000000000010",
"categoryName": "知识管理默认二级分类(1)",
"parentUuid": "category000000000000000000000009",
"describeInfo": "知识管理默认二级分类",
"lever": 2,
"childRen": []
}
]
},
{
"uuid": "z4440a5127f0492ab3eaeb1a4a98406f",
"categoryName": "1233(0)",
"parentUuid": "thetopuuid0000000000000000000000",
"describeInfo": "",
"lever": 1,
"childRen": [
{
"uuid": "d5b4dc296a5e47c8bb546fe1ac3a5446",
"categoryName": "3无123213(0)",
"parentUuid": "z4440a5127f0492ab3eaeb1a4a98406f",
"describeInfo": "",
"lever": 2,
"childRen": [
{
"uuid": "i7f477bfbf3f44a797efeae830212320",
"categoryName": "tttt(0)",
"parentUuid": "d5b4dc296a5e47c8bb546fe1ac3a5446"