admin管理员组

文章数量:1559111

发现了画图对于理解的帮助了
解决了一直以来困扰我的树状结构问题, 开心
贴上自己的代码

    public list<district> getdistricts(string tenant) {
        list<district> flatdistricts = mapper.getdistricts(tenant);
        //获取孩子键值对
        map<string, list<district>> childgroup = flatdistricts.stream().filter(f -> f.getparentcode() != null).collect(collectors.groupingby(district::getparentcode));
        //获取所有父元素
        list<district> districts = flatdistricts.stream().filter(f->f.getparentcode() == null).collect(collectors.tolist());
        arraylist<district> parents = new arraylist<>(districts);
        for (int i = 0; i < parents.size(); i) {
            district currentparentdistrict = parents.get(i);
            list<district> childrendistricts = childgroup.get(currentparentdistrict.getcode());
            if(childrendistricts != null) {
                currentparentdistrict.setchildren(childrendistricts);
                parents.addall(childrendistricts);
            }
        }
        return districts;
    }

本文标签: