JAVA后台转换成树结构数据返回给前端

我们会经常用到树形,那么树形结构的数据是在前端做还是在后台做呢?我自己用过前端的ztree,selectTree等这些属于前端的组件,后台只需要把一个表的所有数据返回给前段就可以,前端可以通过id,pid来把层级结构划分,要是我们前端需要后台直接返回树结构数据怎么办,那么接下来我给大家介绍一下我写过的例子。

我们先看一张图了解一下树结构:我这里随便找一张图了解一下即可
在这里插入图片描述
接下来我们写一个小例子,用递归方式转换为数

实体:

package org.inlighting.handwritingjwt;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.io.Serializable;
import java.util.List;

/**
 * 实体类
 *
 * @author Yujiaqi
 * @date 2020/12/15 14:03
 */
@Data
@Getter
@Setter
@ToString
public class TestEntity implements Serializable {
    private Integer id;
    private String name;
    private String parentId;
    private List<TestEntity> children;
    
    public TestEntity(){};
    
    public TestEntity(Integer id, String name, String parentId, List<TestEntity> children) {
        this.id = id;
        this.name = name;
        this.parentId = parentId;
        this.children = children;
    }
    
}

service实现类

@RestController
public class Test {
    private static final String json = "["
            + "{\"id\":44116841,\"name\":\"测试部\",\"parentId\":56188932},"
            + "{\"id\":56104925,\"name\":\"飞轮储能事业部\",\"parentId\":null},"
            + "{\"id\":56121882,\"name\":\"高速电机部\",\"parentId\":56104925},"
            + "{\"id\":44941198,\"name\":\"项目管理部\",\"parentId\":null},"
            + "{\"id\":35232631,\"name\":\"视觉算法\",\"parentId\":19693688},"
            + "{\"id\":17387338,\"name\":\"人力行政部\",\"parentId\":null},"
            + "{\"id\":35197588,\"name\":\"行政\",\"parentId\":17387338},"
            + "{\"id\":19693688,\"name\":\"人工智能部\",\"parentId\":56188932},"
            + "{\"id\":35193567,\"name\":\"人力资源\",\"parentId\":17387338},"
            + "{\"id\":56122856,\"name\":\"结构部\",\"parentId\":56104925},"
            + "{\"id\":54197060,\"name\":\"系统平台部\",\"parentId\":56188932},"
            + "{\"id\":17387341,\"name\":\"财务部\",\"parentId\":null},"
            + "{\"id\":56188932,\"name\":\"机器人事业部\",\"parentId\":null},"
            + "{\"id\":35203611,\"name\":\"自然语言算法\",\"parentId\":19693688}]";

@RequestMapping("/test")
    public List<TestEntity> list(){
        List<TestEntity> testEntitys = JSON.parseObject(json, new TypeReference<List<TestEntity>>(){});
        return TreeUtils.buildByRecursive(testEntitys);
    }

递归方法工具类

package org.inlighting.handwritingjwt;

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

/**
 * @author 于嘉琪
 */
public class TreeUtils {
    /**
     * 使用递归方法建树
     * @param
     * @return
     */
    public static List<TestEntity> buildByRecursive(List<TestEntity> testEntities) {
        List<TestEntity> trees = new ArrayList<>();
        for (TestEntity testEntity : testEntities) {
            if ("".equals(testEntity.getParentId()) || testEntity.getParentId() == null ) {
                trees.add(findChildren(testEntity,testEntities));
            }
        }
        return trees;
    }
    
    /**
     * 递归查找子节点
     * @param
     * @return
     */
    public static TestEntity findChildren(TestEntity testEntity,List<TestEntity> testEntitys) {
        for (TestEntity projectBasicInfoDTO2 : testEntitys) {
            if(String.valueOf(testEntity.getId()).equals(projectBasicInfoDTO2.getParentId())) {
                if(testEntity.getChildren() == null) {
                    testEntity.setChildren(new ArrayList<>());
                }
                //是否还有子节点,如果有的话继续往下遍历,如果没有则直接返回
                testEntity.getChildren().add(findChildren(projectBasicInfoDTO2,testEntitys));
            }
        }
        return testEntity;
    }
}

查出来的效果

[
    {
        "id": 56104925,
        "name": "飞轮储能事业部",
        "parentId": null,
        "children": [
            {
                "id": 56121882,
                "name": "高速电机部",
                "parentId": "56104925",
                "children": null
            },
            {
                "id": 56122856,
                "name": "结构部",
                "parentId": "56104925",
                "children": null
            }
        ]
    },
    {
        "id": 44941198,
        "name": "项目管理部",
        "parentId": null,
        "children": null
    },
    {
        "id": 17387338,
        "name": "人力行政部",
        "parentId": null,
        "children": [
            {
                "id": 35197588,
                "name": "行政",
                "parentId": "17387338",
                "children": null
            },
            {
                "id": 35193567,
                "name": "人力资源",
                "parentId": "17387338",
                "children": null
            }
        ]
    },
    {
        "id": 17387341,
        "name": "财务部",
        "parentId": null,
        "children": null
    },
    {
        "id": 56188932,
        "name": "机器人事业部",
        "parentId": null,
        "children": [
            {
                "id": 44116841,
                "name": "测试部",
                "parentId": "56188932",
                "children": null
            },
            {
                "id": 19693688,
                "name": "人工智能部",
                "parentId": "56188932",
                "children": [
                    {
                        "id": 35232631,
                        "name": "视觉算法",
                        "parentId": "19693688",
                        "children": null
                    },
                    {
                        "id": 35203611,
                        "name": "自然语言算法",
                        "parentId": "19693688",
                        "children": null
                    }
                ]
            },
            {
                "id": 54197060,
                "name": "系统平台部",
                "parentId": "56188932",
                "children": null
            }
        ]
    }
]

以上为后台处理树结构小demo,重点为递归那个工具类

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
将List数据转换成形结构的基本思路是首先遍历List,将其中每个节点的父子关系建立起来,然后从根节点开始递归构建整个形结构。 具体实现步骤如下: 1. 定义节点类,包含节点id、节点名称和子节点列表等属性。 ``` public class TreeNode { private String id; private String name; private List<TreeNode> children; // getter和setter方法 // ... } ``` 2. 遍历List,将每个节点的父子关系建立起来,可以使用Map来存储节点id和对应的节点对象,便于查找父节点。 ``` Map<String, TreeNode> map = new HashMap<>(); for (TreeNode node : list) { map.put(node.getId(), node); String parentId = node.getParentId(); if (parentId != null) { TreeNode parent = map.get(parentId); if (parent != null) { parent.getChildren().add(node); } } } ``` 3. 找到根节点,开始递归构建整个形结构。 ``` public static TreeNode buildTree(List<TreeNode> list) { // 构建Map,方便查找节点 Map<String, TreeNode> map = new HashMap<>(); for (TreeNode node : list) { map.put(node.getId(), node); } // 找到根节点 TreeNode root = null; for (TreeNode node : list) { if (node.getParentId() == null) { root = node; break; } } // 从根节点开始递归构建整个形结构 buildSubTree(root, map); return root; } private static void buildSubTree(TreeNode node, Map<String, TreeNode> map) { List<TreeNode> children = node.getChildren(); if (children == null) { return; } // 遍历子节点,递归构建子 for (TreeNode child : children) { buildSubTree(child, map); } // 根据子节点的顺序重新排序 children.sort(Comparator.comparing(TreeNode::getName)); } ``` 以上就是将List数据转换成行结构的基本实现方法。需要注意的是,这里的代码只是一个简单的示例,实际情况下可能需要根据具体的业务需求进行修改和优化。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值