递归树

原创
04/15 17:29
阅读数 74

1.定义基本树结构

package com.un.common.utils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.List;

@ApiModel("社区结构树")
public class StructTree {
    @ApiModelProperty("社区结构id")
    private String csId;

    @ApiModelProperty("社区id")
    private String csCoId;

    @ApiModelProperty("社区结构parentId")
    private String csParentId;

    @ApiModelProperty("社区结构名称")
    private String csName;

    @ApiModelProperty("社区结构全名称")
    private String csFullName;

    @ApiModelProperty("是否为最后一级,1是 0否")
    private Integer isLast;

    @ApiModelProperty("子树")
    List<com.unwulian.communitystruct.domain.StructTree> structTreeList;

    public String getCsFullName() {
        return csFullName;
    }

    public void setCsFullName(String csFullName) {
        this.csFullName = csFullName;
    }

    public String getCsCoId() {
        return csCoId;
    }

    public void setCsCoId(String csCoId) {
        this.csCoId = csCoId;
    }

    public String getCsName() {
        return csName;
    }

    public void setCsName(String csName) {
        this.csName = csName;
    }

    public String getCsId() {
        return csId;
    }

    public void setCsId(String csId) {
        this.csId = csId;
    }

    public String getCsParentId() {
        return csParentId;
    }

    public void setCsParentId(String csParentId) {
        this.csParentId = csParentId;
    }

    public Integer getIsLast() {
        return isLast;
    }

    public void setIsLast(Integer isLast) {
        this.isLast = isLast;
    }

    public List<com.unwulian.communitystruct.domain.StructTree> getStructTreeList() {
        return structTreeList;
    }

    public void setStructTreeList(List<com.unwulian.communitystruct.domain.StructTree> structTreeList) {
        this.structTreeList = structTreeList;
    }

    @Override
    public String toString() {
        return "StructTree{" +
                "csId='" + csId + '\'' +
                ", csCoId='" + csCoId + '\'' +
                ", csParentId='" + csParentId + '\'' +
                ", csName='" + csName + '\'' +
                ", csFullName='" + csFullName + '\'' +
                ", isLast=" + isLast +
                ", struceTreeList=" + structTreeList +
                '}';
    }
}

 

2.定义工具类:

package com.un.common.utils;

import com.unwulian.communitystruct.domain.StructTree;

import java.util.ArrayList;
import java.util.List;

/**
 * 操作社区结构数操作
 *
 * @author shiye
 * @create 2021-04-15 16:21
 */
public class CsTreeNodeUtils {

    /**
     * 构建数,把社区结构List构建成一颗树
     *
     * @param treeNodes
     * @return
     */
    public static List<com.unwulian.communitystruct.domain.StructTree> buildByRecursive(List<StructTree> treeNodes) {
        List<StructTree> trees = new ArrayList<StructTree>();
        for (StructTree treeNode : treeNodes) {
            if ("CS0".equals(treeNode.getCsParentId())) {
                trees.add(findChildren(treeNode, treeNodes));
            }
        }
        return trees;
    }

    /**
     * 递归添加到指定节点中
     *
     * @param treeNode
     * @param treeNodes
     * @return
     */
    public static com.unwulian.communitystruct.domain.StructTree findChildren(StructTree treeNode, List<StructTree> treeNodes) {
        for (StructTree it : treeNodes) {
            if (treeNode.getCsId().equals(it.getCsParentId())) {
                if (treeNode.getStructTreeList() == null) {
                    treeNode.setStructTreeList(new ArrayList<StructTree>());
                }
                treeNode.getStructTreeList().add(findChildren(it, treeNodes));
            }
        }
        return treeNode;
    }

    /**
     * 返回指定社区下的所有子节点
     *
     * @param csId
     * @param treeList
     * @return
     */
    public static StructTree getTreeNode(String csId, List<StructTree> treeList) {
        for (StructTree structTree : treeList) {
            if (csId.equals(structTree.getCsId())) {
                return structTree;
            } else {
                List<StructTree> tempStructTreeList = structTree.getStructTreeList();
                if(tempStructTreeList != null){
                    StructTree treeNode = getTreeNode(csId, tempStructTreeList);
                    if (treeNode != null) {
                        return treeNode;
                    }
                }
            }
        }
        return null;
    }
}

3.测试方式

//把普通的list结构转换成树节点
        List<StructTree> treeList = CsTreeNodeUtils.buildByRecursive(structTreeList);
        //根据节点中的摸一个节点获取当前节点以及所有子节点
        StructTree treeNode = CsTreeNodeUtils.getTreeNode(csId, treeList);


//跟新选中节点以及其下所有子节点
/**
     * 跟新子节点的所有csFullName
     *
     * @param treeNodeList
     */
    public void updateCsFullName(List<StructTree> treeNodeList, StructTree parentNode) {
        if (CollectionUtils.isEmpty(treeNodeList)) {
            return;
        }
        for (StructTree structTree : treeNodeList) {
            String csFullName = parentNode.getCsFullName() + structTree.getCsName();
            TBasCommunitystruct communitystruct = new TBasCommunitystruct();
            communitystruct.setCsId(structTree.getCsId());
            communitystruct.setCsFullname(csFullName);
            tBasCommunitystructMapper.updateTBasCommunitystruct(communitystruct);
            //当前节点的csFullName也跟新掉
            structTree.setCsFullName(csFullName);
            if (!CollectionUtils.isEmpty(structTree.getStructTreeList())) {
                //自旋跟新子数据节点
                updateCsFullName(structTree.getStructTreeList(), structTree);
            }
        }
    }

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部