java recursion unlimited hierarchical tree - Category Management

First need to paternity, post codes

package com.zhizhuo.zcms.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import java.time.LocalDateTime;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
 * @author zhizhuo-1543057945
 * @since 2019-08-14
 */
@Data
@Accessors(chain = true)
public class Category implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 分类id
     */
    @TableId(value = "cate_id", type = IdType.AUTO)
    private Integer cateId;
    /**
     * 分类名
     */
    private String cateName;
    /**
     * 英文名
     */
    private String cateEn;
    /**
     * 分类添加时间
     */
    private LocalDateTime cateAddTime;
    /**
     * 分类修改时间
     */
    private LocalDateTime cateUpdateTime;
    /**
     * 父id
     */
    private Integer catePid;
    /**
     * 分类排序
     */
    private Integer cateSort;

    /**
     * 分类状态,0开启,1关闭,2为已删除
     */
    private Integer cateStatus;
    /**
     * 分类下的标题-网站标题
     */
    private String cateTitle;
    /**
     * 分类下的网站关键词
     */
    private String cateKey;
    /**
     * 分类下的网站描述
     */
    private String cateDes;
}

Next we create a tree structure treeUtil

package com.zhizhuo.zcms.utils;
import lombok.*;
import java.util.List;

/**
 * 〈一句话功能简述〉<br> 
 * 生成树
 * @author 1543057945
 * @create 2019/8/17
 * @since 1.0.0
 */
@Data
@ToString
@Builder(toBuilder = true)
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class TreeUtil<T> {
    /**
     * 当前节点
     */
    private T currNode;
    /**
     * 孩子
     */
    private List<TreeUtil<T>> childs;
}

He is said about currNode current node, because he is a generic definition, we need to pass the object type can, should think that the category is class above, yes that's, childs just a collection of a TreeUtil, because more than the next node currNode one do, so to define the collection might ask why is not the category of collection, if it is, then there will be problems,

Next, look, we practice
in the project I made a sort of paste sorting code, written using lambda expressions, should be able to understand

 /**
     * 排序
     * @return
     */
    private Comparator<TreeUtil<Category>> sort(){
        return  (o, o1)->o.getCurrNode().getCateSort()-o1.getCurrNode().getCateSort();
    }

Let me talk about the real needs, I want to get all of the following set of specified category id, which is the dynamic tree
then we need to pass an id

 /**
     * 生成树
     * @return
     */
    @Override
    public List<TreeUtil<Category>> getCateTreeById(Integer id) {
        //先得到所有的节点
        List<Category> categoryList = selectAll();
        //把查询到的变成树
        ArrayList<TreeUtil<Category>> treeUtils = new ArrayList<>();
        //排序
        Collections.sort(treeUtils,sort());
        for (Category category1 : categoryList) {
            treeUtils.add(new TreeUtil<Category>().toBuilder().currNode(category1).build());
        }
        //返回该结果的所有树
        return getChild(treeUtils, id);
    }

Core recursive Code

/**
     *
     * @param treeUtilList
     * @param id
     * @return
     */
    @SuppressWarnings("ALL")
    private List<TreeUtil<Category>> getChild(List<TreeUtil<Category>> treeUtilList, Integer id) {
        //装入子树
        ArrayList<TreeUtil<Category>> childs = new ArrayList<>();
        //查找id的子树
        for (TreeUtil<Category> treeUtil : treeUtilList) {
            if (treeUtil.getCurrNode().getCatePid().equals(id)){
                childs.add(treeUtil);
            }
        }
        //排序
        Collections.sort(childs,sort());
        //遍历子树,并查找下一级子树
        for (TreeUtil<Category> child : childs) {
            child.setChilds(getChild(treeUtilList,child.getCurrNode().getCateId()));
        }
        /*//结束条件
        if (childs.size()==0){
            return new ArrayList<>();
        }*/
        return childs;
    }

So we got the tree, by the way posted a picture
Here Insert Picture Description

Published 34 original articles · won praise 6 · views 3654

Guess you like

Origin blog.csdn.net/qq_35986709/article/details/99699161