java stream 生成树形结构

1、entity

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class TutorialsCategory implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    /**
     * 上级类别(0表示一级分类)
     */
    private Long superId;

    /**
     * 名称
     */
    private String name;

    /**
     * 排序,数字越大的越靠后
     */
    private Integer sortCategory;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

}

2、vo

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class TutorialsCategoryTreeVo extends TutorialsCategory implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 设置树下级教程
     */
    private List<TutorialsCategoryTreeVo> childTutorialsCategory=new ArrayList<>();


}
3、得到tree数据
    @RequestMapping(value = "/getTutorialsCategoryTree",method = RequestMethod.POST)
    @RequiresRoles(value={"SUPERMANAGER"},logical = Logical.OR)
    public @ResponseBody
    Object getTutorialsCategoryTree(HttpServletRequest request
    ){
        List<TutorialsCategoryTreeVo> fisrtList=tutorialsService.selectTutorialsAllData(null);
        List<TutorialsCategoryTreeVo> list=fisrtList.stream().filter(tutorialsCategoryTreeVo -> tutorialsCategoryTreeVo.getSuperId()==0L).map((menu)->{
           menu.setChildTutorialsCategory(getChildrens(menu,fisrtList));
           return menu;
        }).collect(Collectors.toList());
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("list",list);
        return map;
    }


    private List<TutorialsCategoryTreeVo> getChildrens(TutorialsCategoryTreeVo root, List<TutorialsCategoryTreeVo> all) {
        List<TutorialsCategoryTreeVo> childrenList = all.stream()
                .filter(categoryVo -> categoryVo.getSuperId().intValue() == root.getId().intValue())
                .map(categoryVo -> {
                    //子菜单可能还有子菜单, 因此递归查询,查询出子菜单  因为当前只有两级,所以不用
                    categoryVo.setChildTutorialsCategory(getChildrens(categoryVo, all));
                   return categoryVo;
                }).collect(Collectors.toList());
        return childrenList;
    }

如果只有两级

    private List<TutorialsCategoryTreeVo> getChildrens(TutorialsCategoryTreeVo root, List<TutorialsCategoryTreeVo> all) {
        List<TutorialsCategoryTreeVo> cList=new ArrayList<>();
        List<TutorialsCategoryTreeVo> childrenList = all.stream()
                .filter(categoryVo -> categoryVo.getSuperId().intValue() == root.getId().intValue())
                .map(categoryVo -> {
                    //子菜单可能还有子菜单, 因此递归查询,查询出子菜单  因为只有两级所以不需要
                    //categoryVo.setChildTutorialsCategory(getChildrens(categoryVo, all));
                   return categoryVo;
                }).collect(Collectors.toList());

              return childrenList;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值