树菜单 递归写法

树菜单 递归写法

返回导航页

返回导航页

1、先整个demo看结果

public static void main(String[] args) {
        List<CategoryEntity> categoryEntities = new ArrayList<>();

        CategoryEntity categoryEntity1 = new CategoryEntity();
        categoryEntity1.setCatId(1L);
        categoryEntity1.setParentCid(0L);
        categoryEntity1.setName("果蔬");
        categoryEntities.add(categoryEntity1);
        CategoryEntity categoryEntity4 = new CategoryEntity();
        categoryEntity4.setCatId(4L);
        categoryEntity4.setParentCid(1L);
        categoryEntity4.setName("香蕉");
        categoryEntities.add(categoryEntity4);
        CategoryEntity categoryEntity5 = new CategoryEntity();
        categoryEntity5.setCatId(5L);
        categoryEntity5.setParentCid(1L);
        categoryEntity5.setName("苹果");
        categoryEntities.add(categoryEntity5);


        CategoryEntity categoryEntity2 = new CategoryEntity();
        categoryEntity2.setCatId(2L);
        categoryEntity2.setParentCid(0L);
        categoryEntity2.setName("肉禽蛋奶");
        categoryEntities.add(categoryEntity2);
        CategoryEntity categoryEntity6 = new CategoryEntity();
        categoryEntity6.setCatId(6L);
        categoryEntity6.setParentCid(2L);
        categoryEntity6.setName("猪肉");
        categoryEntities.add(categoryEntity6);
        CategoryEntity categoryEntity7 = new CategoryEntity();
        categoryEntity7.setCatId(7L);
        categoryEntity7.setParentCid(2L);
        categoryEntity7.setName("鹿肉");
        categoryEntities.add(categoryEntity7);
        CategoryEntity categoryEntity11 = new CategoryEntity();
        categoryEntity11.setCatId(11L);
        categoryEntity11.setParentCid(7L);
        categoryEntity11.setName("鹿鞭");
        categoryEntities.add(categoryEntity11);


        CategoryEntity categoryEntity3 = new CategoryEntity();
        categoryEntity3.setCatId(3L);
        categoryEntity3.setParentCid(0L);
        categoryEntity3.setName("饮料酒水");
        categoryEntities.add(categoryEntity3);
        CategoryEntity categoryEntity8 = new CategoryEntity();
        categoryEntity8.setCatId(8L);
        categoryEntity8.setParentCid(3L);
        categoryEntity8.setName("ROI");
        categoryEntities.add(categoryEntity8);
        CategoryEntity categoryEntity9 = new CategoryEntity();
        categoryEntity9.setCatId(9L);
        categoryEntity9.setParentCid(3L);
        categoryEntity9.setName("茅台");
        categoryEntities.add(categoryEntity9);
        CategoryEntity categoryEntity10 = new CategoryEntity();
        categoryEntity10.setCatId(10L);
        categoryEntity10.setParentCid(9L);
        categoryEntity10.setName("茅台王子");
        categoryEntities.add(categoryEntity10);
        CategoryEntity categoryEntity12 = new CategoryEntity();
        categoryEntity12.setCatId(12L);
        categoryEntity12.setParentCid(9L);
        categoryEntity12.setName("茅台鹿鞭王子酒");
        categoryEntities.add(categoryEntity12);


        List<CategoryEntity> treeMenu = listWithTree(categoryEntities);
        System.out.println(treeMenu);

    }

说明:饮料就说里面有细的分类

饮料酒水— > 茅台 — > 茅台王子鹿鞭酒,茅台王子酒

image-20221129210609654

2、代码:返回对象实体定义

class CategoryEntity implements Serializable {
	private static final long serialVersionUID = 1L;

    /**
     * 分类id
     */
    @TableId
    private Long catId;
//	private Long id;
    /**
     * 分类名称
     */
    private String name;
    /**
     * 父分类id
     */
    private Long parentCid;

    /**
     * 子分类
     * 不是数据表里的属性
     * 将当前菜单的子分类都保存到里面
     * */
    @TableField(exist =false)
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<CategoryEntity> children;
    /**
     * 层级
     */
    private Integer catLevel;
    /**
     * 是否显示[0-不显示,1显示]
     */
    private Integer showStatus;
    /**
     * 排序
     */
    private Integer sort;
    /**
     * 图标地址
     */
    private String icon;
    /**
     * 计量单位
     */
    private String productUnit;
    /**
     * 商品数量
     */
    private Integer productCount;

}

3、代码:递归构造返回实体


public class CategoryServiceImpl {
     public static void main(String[] args) {
     // 把上面main方法内容放过来吧
    }

     public static List<CategoryEntity> listWithTree(List<CategoryEntity> categoryEntities) {
           // 1 查出所有分类
   //        List<CategoryEntity> categoryEntities = new ArrayList<>();
           // 2 组装成父子的树型结构
           // 2.1 找到所有一级分类
           List<CategoryEntity> level1Menus = categoryEntities.stream().filter(
                   // 找到一级
                   categoryEntity -> categoryEntity.getParentCid() == 0
           ).map(menu -> {
               // 把当前的child属性改了之后重新返回
               menu.setChildren(getChildren(menu, categoryEntities));
               return menu;
           })
             /*      .sorted((menu1, menu2) ->
                   menu1.getSort() - menu2.getSort())
                   */
                   .collect(Collectors.toList());
   
           return level1Menus;
   //        return categoryEntities;
       }
   
       /**
        * @param root
        * @param all
        * @return java.util.List<com.atguigu.gulimall.product.entity.CategoryEntity>
        * @Author 骑蜗牛的羊
        * @Description:获取某一个菜单的子菜单 * 在all里找root的子菜单
        */
       private static List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> all) {
           List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
               // 找到当前id的子菜单
               return categoryEntity.getParentCid() == root.getCatId();
           }).map(categoryEntity -> {
               // 1 找到子菜单,递归找法
               categoryEntity.setChildren(getChildren(categoryEntity, all));
               return categoryEntity;
           })
            /*       .sorted((menu1, menu2) -> {
               // 2 菜单排序
               return menu1.getSort() - menu2.getSort();
               // menu1.getSort()==null?0;menu1.getSort()
           })*/
                   .collect(Collectors.toList());
           return children;
       }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码头薯条Pro

本文能帮到阁下,在下很开心!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值