简易版

public List queryWithTree() {
//所有数据
List entityList = baseMapper.selectList(null);

List collect = entityList.stream().filter(item -> {
return item.getParentCid() == 0; //过滤,找出所有没有父级的
}).map(menu -> {
menu.setChildList(getChildList(menu, entityList));//递归将设置子菜单
return menu;
}).sorted(Comparator.comparingInt(item -> (item.getSort() == null ? 0 :
item.getSort()))).collect(Collectors.toList());//排序
return collect;
}

private List getChildList(CategoryEntity menu, List entityList) {

List collect = entityList.stream().filter(item -> {
return item.getParentCid() == menu.getCatId();
}).map(child -> {
child.setChildList(getChildList(child, entityList));
return child;
}).sorted(Comparator.comparingInt(item -> (item.getSort() == null ? 0 : item.getSort()))).collect(Collectors.toList());
return collect;
}

posted @ 2023-11-13 11:28  指尖的键舞  阅读(69)  评论(0)    收藏  举报