java递归方法示例

欣喜 Java经验 发布时间:2024-01-19 16:46:46 阅读数:10048 1
下文笔者讲述java递归方法测试,如下所示 例:
 
@Override
public list getCateGoryTree() {

List categoryEntities = baseDao.selectList(null);

    List<CategoryEntity> collect = categoryEntities.stream().filter(entity ->
            entity.getParentCid() == 0  // 获取父类
    ).map(menu->{ // 映射子类
         menu.setChild(getChildMenu(menu,categoryEntities)); // 调用递归方法子类
         return menu;
    }).sorted((m1,m2)->{
        return ( m1.getSort()==null?0:m1.getSort())-(m2.getSort()==null?0:m2.getSort());
    }).collect(Collectors.toList());
    return collect;
}


//递归子类
private List<CategoryEntity> getChildMenu(CategoryEntity root,List<CategoryEntity> all){
    List<CategoryEntity> collect = all.stream().filter(entity -> {
        return entity.getParentCid() == root.getCatId();
    }).map(entity->{
        entity.setChild(getChildMenu(entity,all)); // 递归获取层级子类
        return entity;
    }).sorted((m1,m2)->{
        return (m1.getSort()==null?0:m1.getSort())-(m2.getSort()==null?0:m2.getSort());
    }).collect(Collectors.toList());
    return collect;
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202401/17056546817711.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者