递归树「建议收藏」

递归树「建议收藏」1.定义基本树结构 package com.un.common.utils; import io.swagger.annotations.ApiModel; import io.swagger.an…

递归树

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/6343.html

(0)
上一篇 2023-04-12
下一篇 2023-04-12

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注