關於JAVA遞歸遍歷樹級菜單結構


需求一:這種不需要傳任何參數

一、數據庫存儲的菜單結果:

 

 

 parentid為0的都是根節點,也就是一級菜單,后面的子菜單的parentid為父菜單的ID。

 

二、MenuDTO類(菜單類)的結構:

@Date
public class MenuDTO {

    private Integer id;

    private String content;

    private Integer parentid;

    private Date createtime;

    private Integer num;

    private List<MenuDTO> childs;

 

三、業務層:采用遞歸方法,遍歷成樹級結構菜單

 //獲得樹級結構菜單
 public List<MenuDTO> getMenuList() throws IOException {
        //拿到菜單的所有數據
        List<MenudTO> list=menuMapper.getMenuList();
        //存儲根節點的菜單,即一級菜單
        List<MenuDTO> rootlist=new ArrayList<>();
        //遍歷所有數據,找到根節點菜單
        for (MenuDTO menuDTO:
             list) {
            if(menuDTO.getParentid().equals(0)){
                //找到根節點菜單的時候,尋找這個根節點菜單下的子節點菜單。
                findChilds(menuDTO,list);
                //添加到根節點的列表中
                rootlist.add(menuDTO);
            }
        }
        return rootlist;
    }

    private void findChilds(MenuDTO root,List<MenuDTO> list){
        List<MenuDTO> childlist=new ArrayList<>();
        //遍歷所有數據,找到是入參父節點的子節點的數據,然后加到childlist集合中。
        for (MenuDTO menu :
                list) {
            if (root.getId().equals(menu.getParentid()))
                childlist.add(menu);
        }
        //若子節點不存在,那么就不必再遍歷子節點中的子節點了 直接返回。
        if(childlist.size()==0)
            return;
        //設置父節點的子節點列表
        root.setChilds(childlist);
        //若子節點存在,接着遞歸調用該方法,尋找子節點的子節點。
        for (MenuDTO childs :
                childlist) {
            findChilds(childs, list);
        }
    }

 

需求二:這種需要傳任何參數,可以傳多個

一、分類實體類

public class ChildNodeCategoryDto {

    /**
     * 分類ID
     */
    private Integer catId;

    /**
     * 分類父ID
     */
    private Integer parentId;

    /**
     * 分類名稱
     */
    private String catName;
    
    /**
     * 分類級別
     */
    private String catLevel;
    
    /**
     * 分類縮略圖
     */
    private String catThumb;
    
    /**
     * 子分類列表
     */
    List<ChildNodeCategoryDto> childCategory = new ArrayList<ChildNodeCategoryDto>();

 

二、業務層:采用遞歸方法,遍歷成樹級結構分類

public List<ChildNodeCategoryDto> getGoodsCategory(String ids) {
        
        List<ChildNodeCategoryDto> list = new ArrayList<ChildNodeCategoryDto>();
        GoodsCategoryDto dto = new GoodsCategoryDto();
        //查詢所有的分類
        dto.setPlatformCode("0001");
        dto.setIsShow(1);
        List<EcsCategory> ecsCategoryList = ecsCategoryMapper.findAllByShowAndPlatformCodeOrderBySortOrder(dto);
        for (EcsCategory ecsCategory : ecsCategoryList) {
            ChildNodeCategoryDto childNodeCategory = new ChildNodeCategoryDto();
            childNodeCategory.setCatId(ecsCategory.getCatId());
            childNodeCategory.setParentId(ecsCategory.getParentId());
            childNodeCategory.setCatName(ecsCategory.getCatName());
            childNodeCategory.setCatLevel(ecsCategory.getCatCode());
            childNodeCategory.setCatThumb(ecsCategory.getCatThumb());
            list.add(childNodeCategory);
        }
        
        //查詢根節點數據
        List<ChildNodeCategoryDto> rootLists = new ArrayList<ChildNodeCategoryDto>();        
   
        String[] strArray = ids.split(",");
        for(int i = 0; i<strArray.length ;i++) {
            Integer catId = Integer.parseInt(strArray[i]);
             //先找到所有的一級菜單
             for (ChildNodeCategoryDto childNodeCategoryResponse : list) {
                  if (childNodeCategoryResponse.getCatId().equals(catId)) {
                        rootLists.add(childNodeCategoryResponse);
                        getChild(childNodeCategoryResponse, list);
                    }
                }
            }
        
        }
        return rootLists;
private void getChild(ChildNodeCategoryDto category, List<ChildNodeCategoryDto> list) {
        // 存放子菜單的集合
        List<ChildNodeCategoryDto> childList = new ArrayList<ChildNodeCategoryDto>();
        category.setChildCategory(childList);
        for (ChildNodeCategoryDto childNodeCategoryResponse : list) {
            if (childNodeCategoryResponse.getParentId().equals(category.getCatId())) {
                childList.add(childNodeCategoryResponse);
                getChild(childNodeCategoryResponse, list);
            }
        }
    }

 

三、總結:

先拿到所有的菜單數據,然后遍歷菜單數據,找到根節點,找到根節點。然后調用找到子節點的方法,在子節點方法中遞歸調用自己,

也就是說如果這個節點有子節點,那么遞歸調用方法找到子節點的子節點,直到某個節點下面沒有子節點。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM