@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; }
标签:return,递归,item2,menu,item1,entities,getSort From: https://www.cnblogs.com/lcmcjq/p/17883977.html