Java递归构建树形数据

文章目录

  • Java递归构建树形数据
  • 一、前言
  • 二、正文

Java递归构建树形数据

一、前言

  • 开发工具:IntelliJ IDEA
  • JDK:1.8

二、正文

  • 添加依赖

<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
    <version>1.18.22version>
    <scope>providedscope>
dependency>

<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>fastjsonartifactId>
    <version>1.2.62version>
dependency>
  • 数据节点类: DataNode
package com.example;

import lombok.Data;
import java.util.List;

/**
 * 数据节点
 * */
@Data
public class DataNode {
    private String id;
    private String pid; // 父节点ID
    private String name;
    private List<DataNode> children; // 子节点列表

    public DataNode(String id, String pid, String name) {
        this.id = id;
        this.pid = pid;
        this.name = name;
    }
}
  • 树形数据工具类:TreeDataUtil

支持单根节点数据、多根节点数据和自定义单根节点数据

package com.example;

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

/**
 * 树形数据工具
 * */
public class TreeDataUtil {

    // 数据源
    private List<DataNode> dataNodes = new ArrayList();
    // 默认父节点ID
    private String defaultPID = "0";

    public TreeDataUtil(List<DataNode> dataNodes) {
        this.dataNodes=dataNodes;
    }

    public TreeDataUtil(List<DataNode> dataNodes, String defaultPID) {
        this.dataNodes = dataNodes;
        this.defaultPID = defaultPID;
    }

    /**
     * 构建单根节点树形结构
     * */
    public List<DataNode> builSingleRootTree(){
        List<DataNode> treeMenus =new  ArrayList<>();
        DataNode dataNode = getSingleRootNode();
        if(dataNode != null){
            dataNode=buildChilTree(dataNode);
            treeMenus.add(dataNode);
        }
        return treeMenus;
    }

    /**
     * 构建多根节点树形结构
     * */
    public List<DataNode> builMultipleRootTree(){
        List<DataNode> treeMenus =new  ArrayList<>();
        for(DataNode dataNode : getMultipleRootNode()) {
            dataNode=buildChilTree(dataNode);
            treeMenus.add(dataNode);
        }
        return treeMenus;
    }

    /**
     * 自定义单根节点树形结构
     * */
    public List<DataNode> builCustomSingleRootTree(String text){
        List<DataNode> treeMenus =new  ArrayList<>();
        DataNode dataNode = new DataNode(defaultPID, "RT01", text);
        dataNode=buildChilTree(dataNode);
        treeMenus.add(dataNode);
        return treeMenus;
    }

    /**
     * 递归,构建子树形结构
     * @param pNode 父节点
     * */
    private DataNode buildChilTree(DataNode pNode){
        List<DataNode> chilMenus =new  ArrayList<>();
        for(DataNode dataNode : dataNodes) {
            if(dataNode.getPid().equals(pNode.getId())) {
                // 递归查询当前节点是否还有子节点
                chilMenus.add(buildChilTree(dataNode));
            }
        }
        pNode.setChildren(chilMenus);
        return pNode;
    }

    /**
     * 获取多根节点
     * */
    private List<DataNode> getMultipleRootNode() {
        List<DataNode> roots =new  ArrayList<DataNode>();
        for(DataNode dataNode : dataNodes) {
            if(dataNode.getPid().equals(defaultPID)) {
                roots.add(dataNode);
            }
        }
        return roots;
    }

    /**
     * 获取单根节点
     * */
    private DataNode getSingleRootNode() {
        DataNode result = null;
        for(DataNode dataNode : dataNodes) {
            if(dataNode.getId().equals(defaultPID)) {
                result = dataNode;
                break;
            }
        }
        return result;
    }
}
  • 测试类:App
package com.example;

import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.List;

public class App
{
    public static void main( String[] args )
    {
        // 测试数据
        List<DataNode> dataNodes= new ArrayList();
        dataNodes.add(new DataNode("0","RT01","商城"));
        dataNodes.add(new DataNode("DN001","0","电器"));
        dataNodes.add(new DataNode("DN002","0","家居"));
        dataNodes.add(new DataNode("DN011","DN001","电视"));
        dataNodes.add(new DataNode("DN012","DN001","空调"));
        dataNodes.add(new DataNode("DN013","DN001","洗衣机"));
        dataNodes.add(new DataNode("DN021","DN002","沙发"));
        dataNodes.add(new DataNode("DN021","DN002","床垫"));
        dataNodes.add(new DataNode("DN0121","DN012","美的"));
        dataNodes.add(new DataNode("DN0122","DN012","格力"));

        // 构建树形数据
        TreeDataUtil treeDataUtil =new TreeDataUtil(dataNodes);
        dataNodes=treeDataUtil.builSingleRootTree(); // 单根节点
        System.out.println(JSON.toJSONString(dataNodes));
        System.out.println("================================");
        dataNodes=treeDataUtil.builMultipleRootTree(); // 多根节点
        System.out.println(JSON.toJSONString(dataNodes));
        System.out.println("================================");
        dataNodes=treeDataUtil.builCustomSingleRootTree("根节点"); // 自定义单根节点
        System.out.println(JSON.toJSONString(dataNodes));
    }
}

你可能感兴趣的:(#,Java,java)