@Override public List<CategoryEntity> listAsTree() { List<CategoryEntity> entities = baseMapper.selectList(null); List<CategoryEntity> level1Menus = entities.stream().filter(categoryEntity -> categoryEntity.getParentCid() == 0 ).map((menu)->{menu.setChildren(getChildren(menu,entities));//收集之前处理再返回 return menu; }).sorted((menu1,menu2)->{ return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());//排序 }).collect(Collectors.toList()); return level1Menus; } public List<CategoryEntity> getChildren(CategoryEntity root,List<CategoryEntity> all){ List<CategoryEntity> children = all.stream().filter((categoryEntity) -> { return categoryEntity.getParentCid() == root.getCatId(); }).map(categoryEntity -> { categoryEntity.setChildren(getChildren(categoryEntity,all)); return categoryEntity; }).sorted((menu1,menu2)->{ return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());//排序 }).collect(Collectors.toList()); return children; }
标签:return,树状,List,categoryEntity,menu2,menu1,封装,节点,getSort From: https://www.cnblogs.com/upupup-999/p/16642304.html