首页 > 其他分享 >递归查询方法

递归查询方法

时间:2022-08-25 21:01:28浏览次数:67  
标签:return 递归 查询方法 List categoryEntity menu2 menu1 getSort

递归查询方法

    /**
     * 获取树形结构的商品数据
     * @return
     */
    @Override
    public  List<CategoryEntity> listWithTree() {
        //1.获取所有的商品分类数据
        List<CategoryEntity> rootCategoryList = baseMapper.selectList(null);
        //2.组装成父子树形结构
        List<CategoryEntity> list = rootCategoryList.stream()
                //过滤出一级分类商品
                .filter(categoryEntity -> categoryEntity.getParentCid().longValue() == 0 )
                .map(categoryEntity -> {
                    categoryEntity.setChildren(getChildren(categoryEntity,rootCategoryList));
                    return categoryEntity;
                })
                .sorted((menu1,menu2)->{return  (menu1.getSort()==null ? 0 : menu1.getSort()) -
                        (menu2.getSort() == null?0:menu2.getSort());})
                .collect(Collectors.toList());
        return list;
    }

    /**
     * 递归查询出所有的子菜单
     * @param root
     * @param allCategories
     * @return
     */
    private List<CategoryEntity> getChildren(CategoryEntity root,List<CategoryEntity> allCategories)
    {
        List<CategoryEntity> children = allCategories.stream()
                .filter(categoryEntity -> categoryEntity.getParentCid().longValue() == root.getCatId().longValue())
                .map(categoryEntity -> {
                    categoryEntity.setChildren(getChildren(categoryEntity, allCategories));
                    return categoryEntity;
                }) .sorted((menu1, menu2) -> {
                    return (menu1.getSort()==null ? 0 : menu1.getSort())  -
                            (menu2.getSort() == null?0:menu2.getSort());})
                .collect(Collectors.toList());
        return children;
    }

标签:return,递归,查询方法,List,categoryEntity,menu2,menu1,getSort
From: https://www.cnblogs.com/seanRay/p/16625679.html

相关文章

  • 分区函数和快排(快排分为递归和非递归两个版本)
    packageclass08;importjava.util.Arrays;importjava.util.Stack;/***分区函数和快排。*快排分为递归和非递归两个版本。*/publicclassCode03_Partitio......
  • 824笔记(闭包,递归,浅/深拷贝)
    闭包闭包:有权访问另一个函数作用域中变量的函数,一个作用域可以访问另外一个函数内部的局部变量作用:延伸了变量的作用范围特性:变量或者参数不会被垃圾回收机制回收函......
  • 2022-8-22 剑指offer-优先队列-每日一题-二叉树-搜索/递归
    剑指OfferII060.出现频率最高的k个数字难度中等36收藏分享切换为英文接收动态反馈给定一个整数数组 nums 和一个整数 k ,请返回其中出现频率前 k 高的元......
  • Linux中的递归参数-r和-p
    Linux中的递归参数-r和-p常见递归有两种:一是从指定目录向上级目录递归。使用-p参数,意为parents,代表命令mkdir等二是从指定目录向其下级目录递归。使用-r参数,意为recurs......
  • 7-17 汉诺塔的非递归实现
    借助堆栈以非递归(循环)方式求解汉诺塔的问题(n,a,b,c),即将N个盘子从起始柱(标记为“a”)通过借助柱(标记为“b”)移动到目标柱(标记为“c”),并保证每个移动符合汉诺塔问题的要求......
  • 使用JS 递归函数 输出 斐波那契数列 (return 返回值使用时的注意点 )
      functionaee(i){  if(i==0){    return0;  }  if(i==1){    return1;  }  if(i>=2){ ***//......
  • 2022-8-20 每日一题-二叉树-递归
    654.最大二叉树难度中等499收藏分享切换为英文接收动态反馈给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建:创建一个......
  • 练习9:尾递归优化
    尾递归和普通递归有啥区别尾调用,是指函数内部的最后一个动作是函数调用。该调用的返回值,直接返回给函数。举个例子://尾调用functionf(x){returng(x);}//非......
  • 2022-8-19 剑指offer-二叉树-递归
    剑指OfferII055.二叉搜索树迭代器难度中等30收藏分享切换为英文接收动态反馈实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代......
  • c语言中利用函数递归求阶乘
     001、#include<stdio.h>inttest(intn)//定义函数{if(n>0){returnn*test(n-1);//调用函数自......