0
点赞
收藏
分享

微信扫一扫

权限树

1.获取树(类似权限树、菜单列表)

1.获取所有的数据列表 :prototypeList

2.声明根节点对象list

3.声明其他节点对象list

4.通过关键字段(等区分上下级关系字段)筛选出根节点数据放到根节点结合对象,以及非根节点数据到其他节点对象集合

5.在getTree方法中操作两个集合对象,先申明一个Maps.newHashMapWithExpectedSize() 用来存放已处理的其他节点对 象

public List<T> tagConfTreeList(List<T> prototypeList) {
    if (CollectionUtils.isEmpty(prototypeList)){
        return null;
    }
    //根节点对象存放到这里
    List<T> rootList = new ArrayList<>();
    //其他节点存放到这里,可以包含根节点
    List<T> bodyList = new ArrayList<>();
    for (T sysTagConf: prototypeList) {
        if(sysTagConf != null) {
            //如果级别是1说明是父节点
            if (sysTagConf.getLevels().equals("0")) {
                //所有父节点数据放进去
                rootList.add(sysTagConf);
            } else {
                //其他节点数据也放进去
                bodyList.add(sysTagConf);
            }
        }
    }
    //组装的树返给前端
    List<T> stc = getTree(rootList,bodyList);
    return stc;
}
public List<T> tagConfTreeList(List<T> prototypeList,String rootLvl) {
    if (CollectionUtils.isEmpty(prototypeList)){
        return null;
    }
    //根节点对象存放到这里
    List<T> rootList = new ArrayList<>();
    //其他节点存放到这里,可以包含根节点
    List<T> bodyList = new ArrayList<>();
    for (T sysTagConf: prototypeList) {
        if(sysTagConf != null) {
            //如果级别是1说明是父节点
            if (sysTagConf.getLevels().equals(StringUtils.isEmpty(rootLvl)?"1":rootLvl)) {
                //所有父节点数据放进去
                rootList.add(sysTagConf);
            } else {
                //其他节点数据也放进去
                bodyList.add(sysTagConf);
            }
        }
    }
    //组装的树返给前端sb
    List<T> stc = getTree(rootList,bodyList);
    return stc;
}
private  List<T> getTree(List<T> rootList,List<T> 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;
}

private  void getChild(T treeDto,Map<String,String> map,List<T> bodyList){
    List<T> childList = Lists.newArrayList();
    bodyList.stream()
            .filter(c -> !map.containsKey(c.getCurrentId()))
            .filter(c ->c.getParentId().equals(treeDto.getCurrentId()))
            .forEach(c ->{
                map.put(c.getCurrentId(),c.getParentId());
                getChild(c,map,bodyList);
                childList.add(c);
            });
    //子数据集
    treeDto.setChildrenList(childList);
}


举报

相关推荐

0 条评论