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

匿名 (未验证) 提交于 2019-12-02 21:40:30

话不多说直接上菜

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返回效果

  

文章来源: https://blog.csdn.net/chendu500qiang/article/details/91493147
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!