stream流 树形结构(不用递归)

@Override
public List getDepTree() {

    LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>();
    lambdaQueryWrapper.eq(ShopAdminDepartment::getIsDelete, BaseEnum.DELETE_NO.getCode());

    List departmentList = this.list(lambdaQueryWrapper);


    List collect = departmentList.stream().map(v -> CglibUtil.copy(v, ShopAdminDepartmentTreeDTO.class)).collect(Collectors.toList());


    //找出级别最高的节点(数字越小级别越高)
    List rootList = collect.stream().filter(v -> "0".equals(v.getParentId())).collect(Collectors.toList());

    Map permissionTreeDTOMap = collect.stream().collect(Collectors.toMap(ShopAdminDepartmentTreeDTO::getId, v -> v,(k,v)->k));

    collect.forEach(v -> {
        
    
        if (permissionTreeDTOMap.containsValue(v.getParentId())) {

            List children = permissionTreeDTOMap.get(v.getParentId()).getChildren();
            children.add(v);
        }
    });

    return rootList;
}

你可能感兴趣的:(java,开发语言)