package com.example.api.Utils.TreeTools;
import com.example.api.Model.TreeMenuTable;
import java.util.ArrayList;
import java.util.List;
/**
* 导航菜单工具类
*/
public class TreeTools {
/**
* 递归生成方法
* @param {menuParentId}
* @param {menus}
* @return
*/
public static List getTreeList(List children) {
List resultList = new ArrayList<>();
//获取顶层元素集合
int parentCode;
for (TreeMenuTable entity : children) {
parentCode = entity.getMenuParentId();
//顶层元素的parentCode==null或者为0
if (parentCode == 0) {
resultList.add(entity);
}
}
//获取每个顶层元素的子数据集合
for (TreeMenuTable entity : resultList) {
entity.setChildren(getSubList(entity.getMenuId(), children));
}
return resultList;
}
/**
* 获取子数据集合
*
* @param id
* @param entityList
* @return
* @author Lastly
* @date 2020年12月29日
*/
private static List getSubList(int id, List entityList) {
List children = new ArrayList<>();
int parentId;
//子集的直接子对象
for (TreeMenuTable entity : entityList) {
parentId = entity.getMenuParentId();
if (id == parentId) {
children.add(entity);
}
}
//子集的间接子对象
for (TreeMenuTable entity : children) {
entity.setChildren(getSubList(entity.getMenuId(), entityList));
}
//递归退出条件
if (children.size() == 0) {
return null;
}
return children;
}
}
最后
以上就是唠叨萝莉最近收集整理的关于range tree java_Java递归生成树形菜单数据的全部内容,更多相关range内容请搜索靠谱客的其他文章。
发表评论 取消回复