调用
List<MenuTreeVo> result= buildTree(menuTreeVos, null)
递归方法
private List<MenuTreeVo> buildTree(List<MenuTreeVo> list, String pid) {
List<MenuTreeVo> tree = new ArrayList<>();
for (MenuTreeVo node : list) {
if (Objects.equals(node.getParentId(), pid)) {
tree.add(findChild(node, list));
}
}
tree.sort(Comparator.comparing(MenuTreeVo::getOrderNo));
return tree;
}
private MenuTreeVo findChild(MenuTreeVo node, List<MenuTreeVo> list) {
for (MenuTreeVo n : list) {
if (Objects.equals(n.getParentId(), node.getUuid())) {
if (node.getChildren() == null) {
node.setChildren(new ArrayList<MenuTreeVo>());
}
node.getChildren().add(findChild(n, list));
}
}
if (node.getChildren() != null) {
node.getChildren().sort(Comparator.comparing(MenuTreeVo::getOrderNo));
}
return node;
}
MenuTreeVo 类
@Data
public class MenuTreeVo implements Serializable {
/**
*
*/
private String uuid;
/**
* 菜单名称
*/
private String name;
/**
* 路径
*/
private String routePath;
/**
* 路由
*/
private String component;
/**
* 菜单标题
*/
private String title;
/**
* 是否固定菜单
*/
private Boolean affix;
/**
* 图标
*/
private String icon;
/**
* 排序
*/
private Integer orderNo;
/**
*
*/
private String parentId;
private List<MenuTreeVo> Children;
/*
* 是否选中
* */
private Boolean isSelected;
}