list转不限层级的tree

2019-10-09  本文已影响0人  更真实

效果

根节点 子节点

数据结构

数据结构

实体

public class Org implements Serializable{
    private static final long serialVersionUID = -3558400362241449714L;
    @JsonProperty("key")
    private int id;
    private int parentId;
    @JsonIgnore
    private Org parentOrg;
    private String code;
    @JsonProperty("title")
    private String name;
    private int order;
    private List<Integer> childrenIds;// 存放子节点ID,以及子节点的下层节点ID
    @JsonProperty("children")
    private List<Org> childrens;// 存放直接子节点实体
    private Date createtime;
    private String createBy;
    private Date modifyTime;
    private String modifyBy;
    
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getParentId() {
        return parentId;
    }
    public void setParentId(int parentId) {
        this.parentId = parentId;
    }
    
    public Org getParentOrg() {
        return parentOrg;
    }
    public void setParentOrg(Org parentOrg) {
        this.parentOrg = parentOrg;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    public List<Integer> getChildrenIds() {
        return childrenIds;
    }
    public void setChildrenIds(List<Integer> childrenIds) {
        this.childrenIds = childrenIds;
    }
    public List<Org> getChildrens() {
        return childrens;
    }
    public void setChildrens(List<Org> childrens) {
        this.childrens = childrens;
    }
    public Date getCreatetime() {
        return createtime;
    }
    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }
    public String getCreateBy() {
        return createBy;
    }
    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }
    public Date getModifyTime() {
        return modifyTime;
    }
    public void setModifyTime(Date modifyTime) {
        this.modifyTime = modifyTime;
    }
    public String getModifyBy() {
        return modifyBy;
    }
    public void setModifyBy(String modifyBy) {
        this.modifyBy = modifyBy;
    }
    public int getOrder() {
        return order;
    }
    public void setOrder(int order) {
        this.order = order;
    }
    /**
     * 添加子节点
     * @param org
     */
    public void addChildren(Org org) {
        if(this.childrens==null) {
            this.childrens=new ArrayList<Org>();
        }
        this.addChildrenId(org.getId());
        this.childrens.add(org);
    }
    /**
     * 添加ID到子节点ID集合,如果此节点存在父节点则把ID添加到父节点的子节点ID集合
     * @param id
     */
    private void addChildrenId(int id) {
        if(this.childrenIds==null) {
            this.childrenIds=new ArrayList<Integer>();
        }
        if(this.parentOrg!=null) {
            this.parentOrg.addChildrenId(id);
        }
        this.childrenIds.add(id);
    }
    public Org() {
        super();
    }
    @Override
    public String toString() {
        return "Org [id=" + id + ", parentId=" + parentId + ", code=" + code + ", name=" + name + ", order=" + order
                + ", createtime=" + createtime + ", createBy=" + createBy + ", modifyTime=" + modifyTime + ", modifyBy="
                + modifyBy + "]";
    }
    
}

list转tree

@Service
public class OrgServiceImpl implements OrgService{
    @Autowired
    private OrgMapper orgMapper;
    public List<Map<String, Object>> query(int pid){
        return null;
    }
    @Override
    public Org add(Org org) throws Exception {
        return orgMapper.add(org);
    }
    @Override
    public List<Org> queryAll() {
        List<Org> orgs=orgMapper.queryAll();
        Map<Integer, Org> tree=createTree(orgs);
        return new ArrayList<Org>(tree.values());
    }
    

    /**
     * 生成树
     * @return
     */
    private Map<Integer, Org> createTree(List<Org> orgs) {
        Map<Integer, Org> matched=new HashMap<>();// 存放已添加到树中的节点
        List<Org> unmatched=orgs;// 存放未添加到树中的节点
        Map<Integer, Org> tree=new HashMap<>();// 存放根节点,
        addRoot(unmatched,tree,matched);
        int preProcessCount=unmatched.size();
        int processedCount=0;
        while(preProcessCount>processedCount) {
            preProcessCount=unmatched.size();
            unmatched=addNode(unmatched,matched);
            processedCount=unmatched.size();
        }
        return tree;
    }
    /**
     * 添加节点到根
     * @param orgs
     */
    private void addRoot(List<Org> orgs,Map<Integer, Org> tree,Map<Integer, Org> matched) {
        for(Org org:orgs) {
            if(isRoot(org)) {
                tree.put(org.getId(), org);
                matched.put(org.getId(), org);
            }
        }
    }
    /**
     * 添加节点到树
     * @param unmatched 未存在树中的节点
     * @param matched 节点已存在树中
     * @return
     */
    private List<Org> addNode(List<Org> unmatched,Map<Integer, Org> matched) {
        List<Org> temp=new ArrayList<>();
        for(Org org : unmatched) {
            if(matched.containsKey(org.getParentId())) {
                Org parent=matched.get(org.getParentId());
                org.setParentOrg(parent);
                parent.addChildren(org);
                matched.put(org.getId(), org);
            }else {
                temp.add(org);
            }
        }
        return temp;
    }
    /**
     * 是否为根节点
     * @param org
     * @return
     */
    private boolean isRoot(Org org) {
        return org.getParentId()<=0;
    }
    
}

上一篇下一篇

猜你喜欢

热点阅读