三级分类-递归树形结构数据
@Override
public List<CategoryEntity> listWithTree() {
List<CategoryEntity> list = baseMapper.selectList(null);
List<CategoryEntity> collect = list.stream().filter((menu) -> {
return menu.getParentId() == 0;
}).map((menu) -> {
menu.setChildCategoryEntity(getChildren(menu, list));
return menu;
}).sorted((menu1, menu2) -> (
(menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort())
)).collect(Collectors.toList());
return collect;
}
private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> list) {
List<CategoryEntity> collect = list.stream().filter((t) -> {
return t.getParentId() == root.getId();
}).map((menu) -> {
menu.setChildCategoryEntity(getChildren(menu, list));
return menu;
}).sorted((menu1, menu2) -> (
(menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort())
)).collect(Collectors.toList());
return collect;
}