1. 程式人生 > 實用技巧 >關於JAVA遞迴遍歷樹級選單結構

關於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);
            }
        }
    }

三、總結:

先拿到所有的選單資料,然後遍歷選單資料,找到根節點,找到根節點。然後呼叫找到子節點的方法,在子節點方法中遞迴呼叫自己,

也就是說如果這個節點有子節點,那麼遞迴呼叫方法找到子節點的子節點,直到某個節點下面沒有子節點。