您好,欢迎来到世旅网。
搜索
您的当前位置:首页java构建树形结构且统计个数和指定统计某项个数

java构建树形结构且统计个数和指定统计某项个数

来源:世旅网

如果仅仅是构建树形结构,可以看这篇文章。
我这里也有递归去形成树形结构的方法

	public static List<Dept> createTreeValues(List<Dept> deptList) {
	      List<Dept> result = new ArrayList<>();
	      for (Dept dept : deptList) {
	            if (dept.getParentId().equals("0")) {
	                /*从根节点开始构树*/
	                result.add(getChildrenNode(dept, deptList));
	            }
	        }
	       return result;
	 }
        
    /*构树递归*/
    private static Dept getChildrenNode(Dept dept, List<Dept> deptList) {
        for (Dept child : deptList) {
            if (child.getParentId().equals(dept.getNodeId())) {
                /*表示是孩子结点*/
                if (dept.getChildren() == null) {
                    dept.setChildren(new ArrayList<>());
                }
                dept.getChildren().add(getChildrenNode(child, deptList));
            }
        }
        return dept;
    }

这里的入参deptList仅仅是一个有父类id和当前id的list数据。
这样到这里为止我们就能拿到有树形结构的数据。
第一种统计所有子类结点数据:

    public static List<Dept> createTreeValues(List<Dept> deptList) {
        List<Dept> result = new ArrayList<>();
        for (Dept dept : deptList) {
            if (dept.getParentId().equals("0")) {
                /*从根节点开始构树*/
                result.add(getChildrenNode(dept, deptList));
            }
        }

        for (Dept dept : result) {
            int num = 0;
            if (dept.getChildren() != null && dept.getChildren().size() > 0) {
                num = countDept(dept.getChildren(), dept.getChildren().size());
            }
            dept.setNodeNum(num);
        }

        return result;

    }


    /*计数递归*/
    private static int countDept(List<Dept> children, int num) {
        for (Dept child : children) {
            if (!CollectionUtils.isEmpty(child.getChildren())) {
                int size = countDept(child.getChildren(), child.getChildren().size());
                child.setNodeNum(size);
            }
            num = child.getNodeNum() + num;
        }
        return num;
    }

出参数据:

{
    "value":[
        {
            "nodeId":"100",
            "parentId":"0",
            "nodeName":"根节点",
            "nodeNum":19,
            "delFlag":0,
            "type":0,
            "reallyId":null,
            "children":[
                {
                    "nodeId":"435",
                    "parentId":"100",
                    "nodeName":"1111",
                    "nodeNum":18,
                    "delFlag":0,
                    "type":0,
                    "reallyId":null,
                    "children":[
                        {
                            "nodeId":"436",
                            "parentId":"435",
                            "nodeName":"2222",
                            "nodeNum":5,
                            "delFlag":0,
                            "type":0,
                            "reallyId":null,
                            "children":[
                                {
                                    "nodeId":"437",
                                    "parentId":"436",
                                    "nodeName":"333",
                                    "nodeNum":0,
                                    "delFlag":0,
                                    "type":0,
                                    "reallyId":null,
                                    "children":null
                                },
                                {
                                    "nodeId":"445",
                                    "parentId":"436",
                                    "nodeName":"4444",
                                    "nodeNum":0,
                                    "delFlag":0,
                                    "type":0,
                                    "reallyId":null,
                                    "children":null
                                },
                                {
                                    "nodeId":"446",
                                    "parentId":"436",
                                    "nodeName":"55555",
                                    "nodeNum":0,
                                    "delFlag":0,
                                    "type":0,
                                    "reallyId":null,
                                    "children":null
                                },
                                {
                                    "nodeId":"447",
                                    "parentId":"436",
                                    "nodeName":"6666",
                                    "nodeNum":0,
                                    "delFlag":0,
                                    "type":0,
                                    "reallyId":null,
                                    "children":null
                                },
                                {
                                    "nodeId":"448",
                                    "parentId":"436",
                                    "nodeName":"7777",
                                    "nodeNum":0,
                                    "delFlag":0,
                                    "type":0,
                                    "reallyId":null,
                                    "children":null
                                }
                            ]
                        },
                        {
                            "nodeId":"438",
                            "parentId":"435",
                            "nodeName":"8888",
                            "nodeNum":1,
                            "delFlag":0,
                            "type":0,
                            "reallyId":null,
                            "children":[
                                {
                                    "nodeId":"439",
                                    "parentId":"438",
                                    "nodeName":"9999",
                                    "nodeNum":0,
                                    "delFlag":0,
                                    "type":0,
                                    "reallyId":null,
                                    "children":null
                                }
                            ]
                        },
                        {
                            "nodeId":"441",
                            "parentId":"435",
                            "nodeName":"10000",
                            "nodeNum":0,
                            "delFlag":0,
                            "type":0,
                            "reallyId":null,
                            "children":null
                        },
                        {
                            "nodeId":"442",
                            "parentId":"435",
                            "nodeName":"10001",
                            "nodeNum":0,
                            "delFlag":0,
                            "type":0,
                            "reallyId":null,
                            "children":null
                        },
                        {
                            "nodeId":"443",
                            "parentId":"435",
                            "nodeName":"10002",
                            "nodeNum":0,
                            "delFlag":0,
                            "type":0,
                            "reallyId":null,
                            "children":null
                        },
                        {
                            "nodeId":"444",
                            "parentId":"435",
                            "nodeName":"10003",
                            "nodeNum":4,
                            "delFlag":0,
                            "type":0,
                            "reallyId":null,
                            "children":[
                                {
                                    "nodeId":"449",
                                    "parentId":"444",
                                    "nodeName":"10004",
                                    "nodeNum":0,
                                    "delFlag":0,
                                    "type":0,
                                    "reallyId":null,
                                    "children":null
                                },
                                {
                                    "nodeId":"450",
                                    "parentId":"444",
                                    "nodeName":"1005",
                                    "nodeNum":0,
                                    "delFlag":0,
                                    "type":0,
                                    "reallyId":null,
                                    "children":null
                                },
                                {
                                    "nodeId":"451",
                                    "parentId":"444",
                                    "nodeName":"10006",
                                    "nodeNum":0,
                                    "delFlag":0,
                                    "type":0,
                                    "reallyId":null,
                                    "children":null
                                },
                                {
                                    "nodeId":"452",
                                    "parentId":"444",
                                    "nodeName":"1007",
                                    "nodeNum":0,
                                    "delFlag":0,
                                    "type":0,
                                    "reallyId":null,
                                    "children":null
                                }
                            ]
                        },
                        {
                            "nodeId":"453",
                            "parentId":"435",
                            "nodeName":"1008",
                            "nodeNum":0,
                            "delFlag":0,
                            "type":0,
                            "reallyId":null,
                            "children":null
                        },
                        {
                            "nodeId":"4",
                            "parentId":"435",
                            "nodeName":"10009",
                            "nodeNum":0,
                            "delFlag":0,
                            "type":0,
                            "reallyId":null,
                            "children":null
                        }
                    ]
                }
            ]
        }
    ]
}

第二种。统计指定属性结点(这里以统计员工为例)

    public static List<Dept> createUserTreeValues(List<Dept> treeList) {

        /*先构树*/
        List<Dept> result = new ArrayList<>();
        for (Dept dept : treeList) {
            if (dept.getParentId().equals("dept-0")) {
                /*从根节点开始构树*/
                result.add(getChildrenNode(dept, treeList));
            }
        }

        /*再统计人数*/
        for (Dept dept : result) {
            int num = 0;
            if (dept.getChildren() != null && dept.getChildren().size() > 0) {
                num = getNodeNum(dept.getChildren(), num);
            }
            dept.setNodeNum(num);
        }

        return result;
    }

    /*只统计员工人数 不统计部门数*/
    private static int getNodeNum(List<Dept> children, int num) {
        for (Dept child : children) {
            if (child.getType() == 2) {
                /*表示当前节点是人,开始累加*/
                num = num + 1;
            } else {
                if (child.getChildren() != null && child.getChildren().size() > 0){
                    /*如果节点不是人,继续遍历*/
                    child.setNodeNum( getNodeNum(child.getChildren(), 0));
                }
                /*计数累加*/
                num =  child.getNodeNum() + num;

            }

        }
        return num;
    }

出参数据:

{
    "value": [
        {
            "nodeId": "dept-100",
            "parentId": "dept-0",
            "nodeName": "根节点",
            "nodeNum": 3,
            "delFlag": 0,
            "type": 1,
            "reallyId": "100",
            "children": [
                {
                    "nodeId": "dept-435",
                    "parentId": "dept-100",
                    "nodeName": "1111",
                    "nodeNum": 3,
                    "delFlag": 0,
                    "type": 1,
                    "reallyId": "435",
                    "children": [
                        {
                            "nodeId": "dept-436",
                            "parentId": "dept-435",
                            "nodeName": "2222",
                            "nodeNum": 1,
                            "delFlag": 0,
                            "type": 1,
                            "reallyId": "436",
                            "children": [
                                {
                                    "nodeId": "dept-437",
                                    "parentId": "dept-436",
                                    "nodeName": "3333",
                                    "nodeNum": 0,
                                    "delFlag": 0,
                                    "type": 1,
                                    "reallyId": "437",
                                    "children": null
                                },
                                {
                                    "nodeId": "dept-445",
                                    "parentId": "dept-436",
                                    "nodeName": "4444",
                                    "nodeNum": 0,
                                    "delFlag": 0,
                                    "type": 1,
                                    "reallyId": "445",
                                    "children": null
                                },
                                {
                                    "nodeId": "dept-446",
                                    "parentId": "dept-436",
                                    "nodeName": "5555",
                                    "nodeNum": 0,
                                    "delFlag": 0,
                                    "type": 1,
                                    "reallyId": "446",
                                    "children": null
                                },
                                {
                                    "nodeId": "dept-447",
                                    "parentId": "dept-436",
                                    "nodeName": "6666",
                                    "nodeNum": 0,
                                    "delFlag": 0,
                                    "type": 1,
                                    "reallyId": "447",
                                    "children": null
                                },
                                {
                                    "nodeId": "dept-448",
                                    "parentId": "dept-436",
                                    "nodeName": "7777",
                                    "nodeNum": 1,
                                    "delFlag": 0,
                                    "type": 1,
                                    "reallyId": "448",
                                    "children": [
                                        {
                                            "nodeId": "user-1000056",
                                            "parentId": "dept-448",
                                            "nodeName": "张三",
                                            "nodeNum": 0,
                                            "delFlag": 0,
                                            "type": 2,
                                            "reallyId": "1000056",
                                            "children": null
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "nodeId": "dept-438",
                            "parentId": "dept-435",
                            "nodeName": "8888",
                            "nodeNum": 1,
                            "delFlag": 0,
                            "type": 1,
                            "reallyId": "438",
                            "children": [
                                {
                                    "nodeId": "dept-439",
                                    "parentId": "dept-438",
                                    "nodeName": "9999",
                                    "nodeNum": 1,
                                    "delFlag": 0,
                                    "type": 1,
                                    "reallyId": "439",
                                    "children": [
                                        {
                                            "nodeId": "user-1000063",
                                            "parentId": "dept-439",
                                            "nodeName": "李四",
                                            "nodeNum": 0,
                                            "delFlag": 0,
                                            "type": 2,
                                            "reallyId": "1000063",
                                            "children": null
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "nodeId": "dept-441",
                            "parentId": "dept-435",
                            "nodeName": "1000",
                            "nodeNum": 0,
                            "delFlag": 0,
                            "type": 1,
                            "reallyId": "441",
                            "children": null
                        },
                        {
                            "nodeId": "dept-442",
                            "parentId": "dept-435",
                            "nodeName": "1001",
                            "nodeNum": 1,
                            "delFlag": 0,
                            "type": 1,
                            "reallyId": "442",
                            "children": [
                                {
                                    "nodeId": "user-1000078",
                                    "parentId": "dept-442",
                                    "nodeName": "王二",
                                    "nodeNum": 0,
                                    "delFlag": 0,
                                    "type": 2,
                                    "reallyId": "1000078",
                                    "children": null
                                }
                            ]
                        },
                        {
                            "nodeId": "dept-443",
                            "parentId": "dept-435",
                            "nodeName": "1002",
                            "nodeNum": 0,
                            "delFlag": 0,
                            "type": 1,
                            "reallyId": "443",
                            "children": null
                        },
                        {
                            "nodeId": "dept-444",
                            "parentId": "dept-435",
                            "nodeName": "1003",
                            "nodeNum": 0,
                            "delFlag": 0,
                            "type": 1,
                            "reallyId": "444",
                            "children": [
                                {
                                    "nodeId": "dept-449",
                                    "parentId": "dept-444",
                                    "nodeName": "1004",
                                    "nodeNum": 0,
                                    "delFlag": 0,
                                    "type": 1,
                                    "reallyId": "449",
                                    "children": null
                                },
                                {
                                    "nodeId": "dept-450",
                                    "parentId": "dept-444",
                                    "nodeName": "1005",
                                    "nodeNum": 0,
                                    "delFlag": 0,
                                    "type": 1,
                                    "reallyId": "450",
                                    "children": null
                                },
                                {
                                    "nodeId": "dept-451",
                                    "parentId": "dept-444",
                                    "nodeName": "1006",
                                    "nodeNum": 0,
                                    "delFlag": 0,
                                    "type": 1,
                                    "reallyId": "451",
                                    "children": null
                                },
                                {
                                    "nodeId": "dept-452",
                                    "parentId": "dept-444",
                                    "nodeName": "1007",
                                    "nodeNum": 0,
                                    "delFlag": 0,
                                    "type": 1,
                                    "reallyId": "452",
                                    "children": null
                                }
                            ]
                        },
                        {
                            "nodeId": "dept-453",
                            "parentId": "dept-435",
                            "nodeName": "1008",
                            "nodeNum": 0,
                            "delFlag": 0,
                            "type": 1,
                            "reallyId": "453",
                            "children": null
                        },
                        {
                            "nodeId": "dept-4",
                            "parentId": "dept-435",
                            "nodeName": "1009",
                            "nodeNum": 0,
                            "delFlag": 0,
                            "type": 1,
                            "reallyId": "4",
                            "children": null
                        }
                    ]
                }
            ]
        }
    ]
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- esig.cn 版权所有 湘ICP备2023023988号-3

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务