Java8 List集合转树状结构
/**
* @param menuList 菜单列表
* @param parentId 祖父级ID
**/
private List<SysUserRoleVo> treeList(List<SysUserRoleVo> menuList, Integer parentId) {
return menuList.stream().filter(menu -> menu.getParentId().intValue() == parentId).map(menu -> {
List<SysUserRoleVo> childList = treeList(menuList, menu.getId().intValue());
if (childList != null && childList.size() > 0) {
menu.setItemList(childList);
}
return menu;
}).collect(Collectors.toList());
}
实现效果
[{
"id": 1,
"name": "首页",
"path": "index",
"parentId": 0
"itemList": {
"id": 2,
"name": "首页2"
"path": "index2"
"parentId": 1
"itemList": {
"id": 10,
"name": "首页2"
"path": "index2"
"parentId": 2
"itemList": {
}
}
}
},
{
"id": 3,
"name": "首页",
"path": "index",
"parentId": 0
"itemList": {
"id": 4,
"name": "首页2"
"path": "index2"
"parentId": 3
"itemList": {
}
}
},
......
]