递归查询树形结构菜单

查询树形结构的菜单,并且可以排序

/**
    * <p>Discription: 获取栏目的菜单树结构</p>
    * Created on: 10:24 2019/5/29
    * @author muyuanpei
    */
    @Override
    public List<CmsChannel> getByPid() {
        List<CmsChannel> cmsChannels = cmsChannelMapper.selectByPid(0);
        List<CmsChannel> cmsChannelList = new ArrayList<>();
        for (CmsChannel ch:cmsChannels) {
            CmsChannel channel = this.recursiveTree(ch.getChannelId());
            cmsChannelList.add(channel);
        }
//        Comparator<CmsChannel> comparing = comparing((CmsChannel a) -> a.getPriority());
        cmsChannelList.sort(comparing(CmsChannel::getPriority).thenComparing(CmsChannel::getChannelId));
        return cmsChannelList;
    }

    public CmsChannel recursiveTree(int cid) {
        //根据cid获取节点对象(查询当前节点的对象)
        CmsChannel channel = cmsChannelMapper.queryById(cid);
        //查询cid下的所有子节点(查询当前节点的子节点)
        List<CmsChannel> cmsChannels = cmsChannelMapper.selectByPid(cid);
        cmsChannels.sort(comparing(CmsChannel::getPriority).thenComparing(CmsChannel::getChannelId));
        //遍历子节点
        for(CmsChannel child : cmsChannels){
            CmsChannel ch = recursiveTree(child.getChannelId());//递归
            channel.getChannelList().add(ch);
        }
        return channel;
    }

使用lambda表达式给list排序

/**
 * 正序排序
 */
// 使用Comparator的comparing
Comparator<Apple> comparing = comparing((Apple a) -> a.getWeight());
inventory.sort(comparing((Apple a) -> a.getWeight()));
//或者等价于
inventory.sort(comparing(Apple::getWeight));


/**
 * 逆序排序
 */
// 1、 根据重量逆序排序
inventory.sort(comparing(Apple::getWeight).reversed());  
// 2、如果两个苹果的重量一样重,怎么办?那就再找一个条件进行排序呗
inventory.sort(comparing(Apple::getWeight).reversed().thenComparing(Apple::getColor));

参考:https://www.cnblogs.com/linjiqin/p/3148066.html
https://blog.csdn.net/u012587693/article/details/52474282

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本系列出于AWeiLoveAndroid的分享,在此感谢,再结合自身经验查漏补缺,完善答案。以成系统。 Java基...
    济公大将阅读 5,418评论 1 6
  • hashmap实现的数据结构,数组、桶等。 如图所示 JDK 1.7,是以数组+链表组成的,链表为相同hash的键...
    不需要任何阅读 4,286评论 0 1
  • 以前的一些收藏,后续再整理格式 如何使用git管理代码版本http://www.cocoachina.com/io...
    安好99阅读 4,948评论 0 4
  • 原创链接 一、Java面试题java有多重要,对于做android的我们,不需要多说了,let’s go (1)J...
    李福来阅读 6,908评论 0 5
  • 自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/mast...
    楷桐阅读 3,602评论 0 5

友情链接更多精彩内容