注意事项:
该方法只是用于小型树型结构,当树结构数据较多的时候,递归时间较长,接口反应很慢,会接口调用超时
实体类

代码实现

private Menu treeRoot(List<Menu> sourceList, Menu rootMenu)
{
if (sourceList == null)
{
return null;
}
List<Menu> childList = new ArrayList<>();
for (Menu menu : sourceList)
{
String menuCode = rootMenu.getMenuCode();
String parentCode = menu.getParentCode();
if(menuCode.equals(parentCode))
{
Menu menuChild = treeRoot(sourceList, menu);
childList.add(menuChild);
}
}
if(childList.size()==0)
{
return rootMenu;
}
rootMenu.setChildrens(childList);
return rootMenu;
}
来源:https://blog.csdn.net/qq_37663786/article/details/99674724