递归查询树形结构

青春壹個敷衍的年華 提交于 2019-11-27 12:39:12

注意事项:

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

实体类

代码实现

 

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!