其他分享
首页 > 其他分享> > 递归查询树形结构

递归查询树形结构

作者:互联网

注意事项:

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

实体类

代码实现

 

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;
}

标签:return,递归,Menu,rootMenu,查询,sourceList,树形,menu,childList
来源: https://blog.csdn.net/qq_37663786/article/details/99674724