zoukankan      html  css  js  c++  java
  • 将lits集合转化为树状结构

    一,bean的类型:

    public class DeptListRES {

    /**
    * 子节点
    */
    private List<DeptListRES> children;

    private Integer id;

    /**
    * 部门编码
    */
    private String deptNo;

    /**
    * 部门名称
    */
    private String deptName;

    /**
    * 部门分类,对应字典表dept_type
    */
    private String deptType;

    /**
    * 全名
    */
    private String fullName;

    private Integer parentId;

    /**
    * 所有的父id
    */
    private String parentIds;

    /**
    * 本级排序号
    */
    private Integer treeSort;

    /**
    * 所有级别排序号
    */
    private String treeSorts;

    /**
    * 是否子节点
    */
    private String treeLeaf;

    }
    2.通过数据库查询出list列表,调用方法转化为树状结构
    List<DeptListRES> depts = deptMapper.list(params);
    return formatter(depts);


    /**
    * 将list转化为树状结构
    *
    * @param list
    * @return
    */
    public List<DeptListRES> formatter(List<DeptListRES> list) {
    Map<Integer, DeptListRES> map = new HashMap<Integer, DeptListRES>();
    for (DeptListRES pt : list) {
    //将集合添加进map,key为id,值为对象
    map.put(pt.getId(), pt);
    }
    return getChild(list, map);
    }

    /**
    * 创建树形结构
    *
    * @param childL
    * @param map
    * @return
    */
    private List<DeptListRES> getChild(List<DeptListRES> childL, Map<Integer, DeptListRES> map) {
    //返回的对象
    List<DeptListRES> par = new ArrayList<>();
    for (DeptListRES deptListRES : childL) {
    //获取父节点
    DeptListRES dt = map.get(deptListRES.getParentId());
    //父节点存在,就在父节点里面添加子节点(利用list里面的对象,和map里面的值得对象为同一个来修改children)
    if (dt != null) {
    if (dt.getChildren() == null) {
    dt.setChildren(new ArrayList<>());
    }
    dt.getChildren().add(deptListRES);
    } else {
    //添加父节点
    par.add(deptListRES);
    }
    }
    return par;
    }
  • 相关阅读:
    软件设计文档
    java基础路线与详细知识点
    hdu 2203 亲和串 kmp
    UVALive 6915 J
    UVALive 6911 F
    UVALive 6906 A
    hdu 3746 Cyclic Nacklace KMP
    hdu 1686 Oulipo kmp算法
    hdu1711 Number Sequence kmp应用
    hdu4749 kmp应用
  • 原文地址:https://www.cnblogs.com/anlegou/p/10281381.html
Copyright © 2011-2022 走看看