stream实现返回树状结构数据

//这里只是针对二级分类噢
 @Override
    public List<SubjectNodeTreeVo> getAllSubjects() {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        //查询指定的列
        wrapper.select("id","title","parent_id");
        List<EduSubject> eduSubjects = baseMapper.selectList(wrapper);
        List<SubjectNodeTreeVo> nodes = new ArrayList<>();
		//查出得数据存到Vo对象中 
        eduSubjects.forEach(eduSubject -> {
            SubjectNodeTreeVo node = new SubjectNodeTreeVo();
            BeanUtils.copyProperties(eduSubject,node);
            nodes.add(node);
        });
        //这里通过stream实现返回树状数据
        Map<String, List<SubjectNodeTreeVo>> children = nodes.stream().filter(node -> (!node.getParentId().equals("0")))
            .collect(Collectors.groupingBy(node -> node.getParentId()));
        nodes.forEach(node -> node.setChildren(children.get(node.getId())));
        return nodes.stream().filter(node -> node.getParentId().equals("0")).collect(Collectors.toList());
    }
package com.njxzc.eduService.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import java.util.Date;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * projectName:  online-edu
 * packageName: com.njxzc.eduService.entity.vo
 * date: 2022-02-20 19:26
 * copyright(c) 2020 njxzc qyl
 * @author qyl
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="EduSubject对象", description="课程科目")
public class EduSubject implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "课程类别ID")
    @TableId(value = "id", type = IdType.ID_WORKER_STR)
    private String id;

    @ApiModelProperty(value = "类别名称")
    private String title;

    @ApiModelProperty(value = "父ID")
    private String parentId;

    @ApiModelProperty(value = "排序字段")
    private Integer sort;

    @ApiModelProperty(value = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    private Date gmtCreate;

    @ApiModelProperty(value = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date gmtModified;


}

package com.njxzc.eduService.entity.vo;

import lombok.Data;

import java.util.List;

/**
 * projectName:  online-edu
 * packageName: com.njxzc.eduService.entity.vo
 * date: 2022-02-20 19:16
 * copyright(c) 2020 njxzc qyl
 *
 * @author qyl
 */
@Data
public class SubjectNodeTreeVo {

    /*结点父类id*/
    private String parentId;

    /*结点名称*/
    private String title;

    /*当前节点id*/
    private String id;

    /*孩子结点list*/
    private List<SubjectNodeTreeVo> children;
//返回树状结构
//    private List<SubjectNodeTreeVo> buildTree(List<SubjectNodeTreeVo> nodes) {
//        Map<String, List<SubjectNodeTreeVo>> children = nodes.stream().filter(node -> (!node.getParentId().equals("0")))
//                .collect(Collectors.groupingBy(node -> node.getParentId()));
//        nodes.forEach(node -> node.setChildren(children.get(node.getNodeId())));
//        return nodes.stream().filter(node -> node.getParentId().equals("0")).collect(Collectors.toList());
//    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值