java递归方法示例
下文笔者讲述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; }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。