lcmcjq

导航

递归

@Override public List<CategoryEntity> listWithTree() { //1、查询所有分类 List<CategoryEntity> entities = baseMapper.selectList(null); //2、组装成树状的父子结构 //查出所有的一级分类 List<CategoryEntity> collect = entities.stream().filter(categoryEntity -> categoryEntity.getParentCid() == 0) .map(menu -> { menu.setChildren(getChildrens(menu,entities)); return menu; }).sorted((item1,item2) ->{ return (item1.getSort()== null?0 : item1.getSort()) - (item2.getSort()== null?0 : item2.getSort()); }).collect(Collectors.toList()); return collect; } //递归查找所有菜单的子菜单 private List<CategoryEntity> getChildrens(CategoryEntity menu, List<CategoryEntity> entities) { List<CategoryEntity> childrens = entities.stream().filter(entitie -> entitie.getParentCid().equals(menu.getCatId())) .map(item -> { item.setChildren(getChildrens(item, entities)); return item; }).sorted((item1,item2) ->{ return (item1.getSort() == null ? 0 : item1.getSort()) - (item2.getSort()== null ? 0 : item2.getSort()); }).collect(Collectors.toList()); return childrens; }

posted on 2023-12-07 21:07  孤独的圆  阅读(3)  评论(0编辑  收藏  举报