ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java数据封装成树形结构,多级

2019-06-12 14:01:14  阅读:469  来源: 互联网

标签:封装 String useLevel List private bodyList 树形 Java null


话不多说直接上菜

1,实体类

@Data
public class SysTagConf implements java.io.Serializable{
    private String rowGuid;         //唯一标识
    private String name;            //标签名称
    private String opType;          //授权类型 0全部 1目录清单 2实施清单 3办理项
    private String useLevel;        //使用层级 0不限 2省级 3地市级 4区县级
    private float sort;            //排序
    private String parentGuid;      //父节点标识
    private String bakNote;         //备注
    private String createUId;       //创建人ID
    private String createUName;     //创建人名称
    private String createTime;      //创建时间
    private String updateUId;       //更新人ID
    private String updateUName;     //更新人名称
    private String updateTime;      //更新时间
    private String parentName;      //父节点名称
    private List<SysTagConf> childList;

}

2,业务模块serviceImpl

/**--------------------数据封装树结构代码---------------------------*/
@Override
public List<SysTagConf> tagConfTreeList(String deptId, List<String> opType) {
    SysDepartment sysDepartment = departmentDao.selectDepartmentByUserId(deptId);
    String useLevel = null;//行使层级
    if(sysDepartment != null){
        if(sysDepartment.getUseLevel() != null && !"".equals(sysDepartment.getUseLevel())) {
            useLevel = sysDepartment.getUseLevel();//行使层级赋值
        }
    }
    List<SysTagConf> sysTagConfList = sysTagConfDao.tagConfTreeList(useLevel,opType); //查询所有数据
    List<SysTagConf> rootList = new ArrayList<>(); //根节点对象存放到这里
    List<SysTagConf> bodyList = new ArrayList<>(); //其他节点存放到这里,可以包含根节点
    for (SysTagConf sysTagConf: sysTagConfList) {
        if(sysTagConf != null) {
            if (sysTagConf.getParentGuid().equals("")) {
                rootList.add(sysTagConf);//所有父节点数据放进去
            } else {
                bodyList.add(sysTagConf); //其他节点数据也放进去
            }
        }
    }
    List<SysTagConf> stc = getTree(rootList,bodyList);//组装的树返给前端sb
    return stc;
}

public List<SysTagConf> getTree(List<SysTagConf> rootList,List<SysTagConf> bodyList){
    if(bodyList != null && !bodyList.isEmpty()){
        //声明一个map,用来过滤已操作过的数据
        Map<String,String> map = Maps.newHashMapWithExpectedSize(bodyList.size());
        rootList.forEach(beanTree -> getChild(beanTree,map,bodyList));
        return rootList;
    }
    return null;
}

public void getChild(SysTagConf treeDto,Map<String,String> map,List<SysTagConf> bodyList){
    List<SysTagConf> childList = Lists.newArrayList();
    bodyList.stream()
            .filter(c -> !map.containsKey(c.getRowGuid()))
            .filter(c ->c.getParentGuid().equals(treeDto.getRowGuid()))
            .forEach(c ->{
                map.put(c.getRowGuid(),c.getParentGuid());
                getChild(c,map,bodyList);
                childList.add(c);
            });
    treeDto.setChildList(childList);//子数据集
}
/**--------------------到此结束---------------------------*/

3返回效果

{
    "code": 0,
    "message": "操作成功",
    "data": [
        {
            "rowGuid": "1",
            "name": "1",
            "opType": "1",
            "useLevel": "1",
            "sort": 1,
            "parentGuid": "",
            "bakNote": "1",
            "createUId": "1",
            "createUName": "1",
            "createTime": "1",
            "updateUId": "1",
            "updateUName": "1",
            "updateTime": "1",
            "parentName": null,
            "childList": [
                {
                    "rowGuid": "2",
                    "name": "2",
                    "opType": "1",
                    "useLevel": "1",
                    "sort": 2,
                    "parentGuid": "1",
                    "bakNote": "2",
                    "createUId": "2",
                    "createUName": "2",
                    "createTime": "2",
                    "updateUId": "2",
                    "updateUName": "2",
                    "updateTime": "2",
                    "parentName": null,
                    "childList": [
                        {
                            "rowGuid": "3",
                            "name": "3",
                            "opType": "1",
                            "useLevel": "1",
                            "sort": 3,
                            "parentGuid": "2",
                            "bakNote": "3",
                            "createUId": "3",
                            "createUName": "3",
                            "createTime": "3",
                            "updateUId": "3",
                            "updateUName": "3",
                            "updateTime": "3",
                            "parentName": null,
                            "childList": []
                        }
                    ]
                }
            ]
        }
    ],
    "messageId": null
}

 


 

 

 

标签:封装,String,useLevel,List,private,bodyList,树形,Java,null
来源: https://blog.csdn.net/chendu500qiang/article/details/91493147

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有