@Override
public List<CategoryEntity> listTree() {
//查出所有分类
List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
//2.组装父子的树形结构
//2.1找到所有树形分类的一级分类
List<CategoryEntity> level1Menu = categoryEntities.stream().filter((CategoryEntity) -> {
return CategoryEntity.getParentCid() == 0;
}).map((menu)->{
menu.setChildren(getChildren(menu,categoryEntities));
return menu;
}).sorted((menu1,menu2)->{
return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
}).collect(Collectors.toList());
return level1Menu;
}
//递归查找所有菜单的子菜单
private List<CategoryEntity>getChildren(CategoryEntity root, List<CategoryEntity>all)
{
List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
return categoryEntity.getParentCid() == root.getCatId();
}).map(categoryEntity -> {
//1.找到子菜单
categoryEntity.setChildren(getChildren(categoryEntity,all));
return categoryEntity;
}).sorted((menu1,menu2)->{
//2.菜单排序
return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
}).collect(Collectors.toList());
return children;
}
继上一篇博客 实战拉姆达表达式(雷神nb!!!)
最新推荐文章于 2023-03-16 17:27:21 发布