xiangpei
2025-03-23 b78b10c1b2aa6f615b7a31c1bd159dfac490d410
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.ycl.utils;
 
import com.ycl.domain.vo.ProjectEngineeringVO;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 构建树工具
 *
 * @author:xp
 * @date:2025/3/3 2:37
 */
public class TreeUtil {
 
    /**
     * 为工程构造树
     *
     * @param nodes
     * @return
     */
    public static List<ProjectEngineeringVO> treeForProjectEng(List<ProjectEngineeringVO> nodes) {
        Map<String, ProjectEngineeringVO> nodeMap = new HashMap<>();
        for (ProjectEngineeringVO node : nodes) {
            nodeMap.put(node.getId(), node);
        }
        List<ProjectEngineeringVO> roots = new ArrayList<>();
        for (ProjectEngineeringVO node : nodes) {
            if ("0".equals(node.getParent())) {
                roots.add(node);
            } else {
                ProjectEngineeringVO parentNode = nodeMap.get(node.getParent());
                if (parentNode != null) {
                    parentNode.getChildren().add(node);
                }
            }
        }
        return roots;
    }
 
}