Stream流递归查询部门树

Java 递归查询树是很常见的功能,也有很多写法,小编这里记录stream流递归部门树写法,自从小编用上stream流之后,是爱不释手,的确是个不错的好东西,话不多说,直接上代码

第一步:先创建dept部门表,简单塞点数据
在这里插入图片描述
第二步:创建实体类

package com.example.work.entity;

import lombok.Data;

@Data
public class Dept {
    /**
    * 主键id
    */
    private Integer id;

    /**
    * 部门名称
    */
    private String deptName;

    /**
    * 父级id
    */
    private Integer parentId;
}

第三步:创建部门vo对象

package com.example.work.vo;

import com.example.work.entity.Dept;
import lombok.Data;

import java.util.List;

/**
 * @Description: 部门树vo
 * @Author: lyz
 * @CreateTime: 2024-01-11
 */
@Data
public class DeptTreeVO extends Dept {

    private List<DeptTreeVO> childrenList;
}

第三步:编写controller层

package com.example.work.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.example.work.service.DeptService;
import com.example.work.vo.DeptTreeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: TODO
 * @Author: lyz
 * @CreateTime: 2024-01-11
 */
@RestController
@RequestMapping("dept")
public class DeptController {

    @Autowired
    DeptService deptService;

    /**
     * @Description: 部门树
     * @return R
     **/
    @GetMapping("/getTreeList")
    public R getTreeList(){
        List<DeptTreeVO> treeVOList = deptService.getTreeList();
        return R.ok(treeVOList);
    }
}

第四步:业务层代码逻辑实现

package com.example.work.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.work.dao.DeptMapper;
import com.example.work.entity.Dept;
import com.example.work.service.DeptService;
import com.example.work.vo.DeptTreeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: lyz
 * @CreateTime: 2024-01-11
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {
    @Override
    public List<DeptTreeVO> getTreeList() {
        List<DeptTreeVO> deptTreeVOList = new ArrayList<>();
        List<Dept> list = this.list();
        list.forEach(dept -> {
            DeptTreeVO deptTreeVO = new DeptTreeVO();
            BeanUtils.copyProperties(dept, deptTreeVO);
            deptTreeVOList.add(deptTreeVO);
        });

        //获取父部门信息
        List<DeptTreeVO> deptTreeVOS = deptTreeVOList.stream().filter(x -> x.getParentId() == 0)
                .map(deptTreeVO -> {
                    deptTreeVO.setChildrenList(getChildrenList(deptTreeVO, deptTreeVOList));
                    return deptTreeVO;
                }).collect(Collectors.toList());

        return deptTreeVOS;
    }

    /**
     * @return List<DeptTreeVO> 子部门信息
     * @Description: 递归查询子部门
     * @param[1] root 父部门
     * @param[2] deptTreeVOList 所有部门
     **/
    private List<DeptTreeVO> getChildrenList(DeptTreeVO root, List<DeptTreeVO> deptTreeVOList) {
        List<DeptTreeVO> childrenList = deptTreeVOList.stream().filter(x -> {
            return Objects.equals(x.getParentId(), root.getId());
        }).map(deptTreeVO -> {
            deptTreeVO.setChildrenList(getChildrenList(deptTreeVO, deptTreeVOList));
            return deptTreeVO;
        }).collect(Collectors.toList());

        return childrenList;
    }
}

第五步:启动程序,并调用接口,结果如下
在这里插入图片描述
stream流递归查询部门树,你学会了吗

/** * 根据等级查询类目 * * @param level * @return */ @Override public List queryCategoryTree(Integer level) { //查询当前级别下类目 List list = categoryDAO.list(level); //组装好的类目,返回前端 List categoryTree = new ArrayList(); //所有类目 List allDTOList = new ArrayList(); if (CollectionUtils.isEmpty(list)) { return categoryTree; } for (CategoryDO categoryDO : list) { allDTOList.add(new CategoryTreeDTO().convertDOToDTO(categoryDO)); } //当前等级类目 categoryTree = allDTOList.stream().filter(dto -> level.equals(dto.getLevel())).collect(Collectors.toList()); for (CategoryTreeDTO categoryTreeDTO : categoryTree) { //组装类目为结构 assembleTree(categoryTreeDTO, allDTOList,Constants.CATEGORY_MAX_LEVEL - level); } return categoryTree; } /** * 组装 * * @param categoryTreeDTO * @param allList * @param remainRecursionCount 剩余递归次数 * @return */ public CategoryTreeDTO assembleTree(CategoryTreeDTO categoryTreeDTO, List allList, int remainRecursionCount) { remainRecursionCount--; //最大递归次数不超过Constants.CATEGORY_MAX_LEVEL-level次,防止坏数据死循环 if(remainRecursionCount < 0){ return categoryTreeDTO; } String categoryCode = categoryTreeDTO.getCategoryCode(); Integer level = categoryTreeDTO.getLevel(); //到达最后等级返回 if (Constants.CATEGORY_MAX_LEVEL == level) { return categoryTreeDTO; } //子类目 List child = allList.stream().filter(a -> categoryCode.equals(a.getParentCode())).collect(Collectors.toList()); if (null == child) { return categoryTreeDTO; } categoryTreeDTO.setChildren(child); //组装子类目 for (CategoryTreeDTO dto : child) { assembleTree(dto, allList,remainRecursionCount); } return categoryTreeDTO; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值