Java 线性数据转换为树形结构

网上查了很多关于线性数据转树形结构的文章,这一篇使我受益匪浅,保存备用

工具类

public class TreeUtil{
    
    public static <E extends TreeEntity<E>> List<E> getTreeList(Integer topId, List<E> entityList) {
        List<E> resultList=new ArrayList<>();
        
        //获取顶层元素集合
        Integer parentId;
        for (E entity : entityList) {
            parentId=entity.getParentId();
            if(parentId==null||topId.equals(parentId)){
                resultList.add(entity);
            }
        }
        
        //获取每个顶层元素的子数据集合
        for (E entity : resultList) {
            entity.setChildren(getSubList(entity.getId(),entityList));
        }
        return resultList;
    }
    
    private  static  <E extends TreeEntity<E>>  List<E> getSubList(Integer id, List<E> entityList) {
        List<E> childList=new ArrayList<>();
        Integer parentId;
        
        //子集的直接子对象
        for (E entity : entityList) {
            parentId=entity.getParentId();
            if(id.equals(parentId)){
                childList.add(entity);
            }
        }
        
        //子集的间接子对象
        for (E entity : childList) {
            entity.setChildren(getSubList(entity.getId(), entityList));
        }
        
        //递归退出条件
        if(childList.size()==0){
            return null;
        }
        
        return childList;
    }
}

TreeEntity

public interface TreeEntity<E> {
    public Integer getId();
    public Integer getParentId();
    public void setChildren(List<E> children);
}

Entity

实现TreeEntity<E>接口即可

返回数据展示

{
    "result": false,
    "msg": "查询成功",
    "obj": [
        {
            "id": "1",
            "depNo": "1",
            "depName": "总部",
            "empName": "",
            "pid": null,
            "changeTime": null,
            "children": [
                {
                    "id": "2",
                    "depNo": "11",
                    "depName": "销售部",
                    "empName": null,
                    "pid": "1",
                    "changeTime": null,
                    "children": [
                        {
                            "id": "4",
                            "depNo": "111",
                            "depName": "销售1部",
                            "empName": "张三",
                            "pid": "2",
                            "changeTime": null,
                            "children": null
                        },
                        {
                            "id": "5",
                            "depNo": "112",
                            "depName": "销售2部",
                            "empName": null,
                            "pid": "2",
                            "changeTime": null,
                            "children": null
                        }
                    ]
                },
                {
                    "id": "3",
                    "depNo": "12",
                    "depName": "人力部",
                    "empName": null,
                    "pid": "1",
                    "changeTime": null,
                    "children": [
                        {
                            "id": "6",
                            "depNo": "121",
                            "depName": "人力1部",
                            "empName": null,
                            "pid": "3",
                            "changeTime": null,
                            "children": null
                        },
                        {
                            "id": "7",
                            "depNo": "122",
                            "depName": "人力2部",
                            "empName": null,
                            "pid": "3",
                            "changeTime": null,
                            "children": null
                        }
                    ]
                }
            ]
        }
    ],
    "code": null
}

我把上述代码进行改造,应用到了项目中

有些地方没有彻底搞懂,像Children的set方法,我没明白怎样在实体类的children中存放List数据,希望大神不吝赐教

本文转载于:https://www.cnblogs.com/G-yong/p/9828825.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值