递归实现无限极菜单

2022-09-19 15:19:01 浏览数 (1)

代码语言:javascript复制
public List<Types> buildTree(List<Types> list) {
    //父级(总的)
    List<Types> typesList = new ArrayList<>();
    list.forEach(t -> {
        if (t.getParentId() == null || t.getParentId() == 0) {
            typesList.add(nodeTree(t, list));
        }
    });
    return typesList;
}
//types 父级的对象
public Types nodeTree(Types types, List<Types> list) {
    List<Types> typesList = new ArrayList<>();
    list.forEach(t -> {
        if (t.getParentId() != null && types.getId().equals(t.getParentId())) {
            typesList.add(nodeTree(t, list));
        }
    });
    types.setChildren(typesList);
    return types;
}

0 人点赞