Vo
public class MenuVo {
private String id ;
private String parentId;
private String name;
private List<MenuVo> vos;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<MenuVo> getVos() {
return vos;
}
public void setVos(List<MenuVo> vos) {
this.vos = vos;
}
}
实现逻辑
@Override
public List<MenuVo> selectTree() {
List<MenuVo> menuVos = this.sysMenuDao.selectAll();
if (CollectionUtils.isEmpty(menuVos)){
return new ArrayList<MenuVo>();
}
List<MenuVo> menuBase = menuVos.stream().filter(vo -> MenuConstants.PARENT_ID.equals(vo.getParentId())).collect(Collectors.toList());
menuBase.stream().filter(vo -> !CollectionUtils.isEmpty(menuBase)).forEach(vo -> {
List<MenuVo> menus = iterateMenus(menuVos, vo.getId());
vo.setVos(menus);
});
return menuBase;
}
private List<MenuVo> iterateMenus(List<MenuVo> menuVos, String id) {
List<MenuVo> result = new ArrayList<>();
menuVos.forEach(vo ->{
String menuId = vo.getId();
String parentId = vo.getParentId();
if (StringUtils.isNotBlank(parentId)){
if (parentId.equals(id)) {
List<MenuVo> iterateMenu = iterateMenus(menuVos,menuId);
vo.setVos(iterateMenu);
result.add(vo);
}
}
});
return result;
}