根据关键字检索树结构数据的最上层结构,并递归获取相关下级数据

该代码段展示了如何在Java中使用StreamAPI和集合处理来过滤包含特定关键词的树形结构数据,并递归地构建包含子节点的树结构。方法包括获取包含关键词的节点,找出其上级节点,并填充子节点信息。
摘要由CSDN通过智能技术生成

@Override
public Res<Object> centerTree(String keyword) {
        List<SupportCenterTreeVo> treeList = supportCenterMapper.tree();
        if(treeList.isEmpty()) return Res.createBySuccess(SysConstant.SUPPORT_CENTER_TYPE_SUCCESS, treeList);
            List<SupportCenterTreeVo> treeList1 = getSupportCenterTreesVo2(treeList, keyword);
            return Res.createBySuccess(SysConstant.SUPPORT_CENTER_TYPE_SUCCESS,treeList1);
    }

    private List<SupportCenterTreeVo> getSupportCenterTreesVo2(List<SupportCenterTreeVo> treeList, String keyword){
        //包含关键字的文件夹
        List<SupportCenterTreeVo> filterResourceList =
                treeList.stream().filter(supportCenterTreeVo -> supportCenterTreeVo.getName().contains(keyword))
                        .collect(Collectors.toList());
        if(org.springframework.util.CollectionUtils.isEmpty(filterResourceList)){
            return Collections.emptyList();
        }
        //查询包含关键字的最上层文件夹,及其子文件夹
        List<String> collect1 = filterResourceList.stream().map(SupportCenterTreeVo::getId).collect(Collectors.toList());
        List<SupportCenterTreeVo> supportCenterTreesVos = new ArrayList<>();
        for (SupportCenterTreeVo s : filterResourceList) {
            if(!collect1.contains(s.getPid())){
                SupportCenterTreeVo supportCenterTreeVo = treeList.stream().filter(a -> s.getId().equals(a.getId())).collect(Collectors.toList()).get(0);
                SupportCenterTreeVo hh= getSupportCenterTreesVo3(treeList,supportCenterTreeVo);
                supportCenterTreesVos.add(hh);
            }
        }
        return supportCenterTreesVos

}

private SupportCenterTreeVo getSupportCenterTreesVo3(List<SupportCenterTreeVo> treeList,SupportCenterTreeVo supportCenterTreeVo){
        List<SupportCenterTreeVo> collect = treeList.stream().filter(a -> supportCenterTreeVo.getId().equals(a.getPid())).collect(Collectors.toList());
        supportCenterTreeVo.setChildren(collect);
        if(CollectionUtils.isEmpty(collect)) return supportCenterTreeVo;

        for (SupportCenterTreeVo centerTreeVo : collect) {
            getSupportCenterTreesVo3(treeList,centerTreeVo);
        }
        return supportCenterTreeVo;
    }

附带石姓朋友提供的树形结构查询工具类(可满足树结构数据普通查询)

public class TreeUtils {

public static <T> List<T> buidTree(List<T> list, Class<T> clazz, String id, String parentId, String children) throws Exception{

        Field idField;

        try {

            idField = clazz.getDeclaredField(id);

        } catch (NoSuchFieldException e) {

            idField = clazz.getSuperclass().getDeclaredField(id);

        }

        Field parentField;

        try {

            parentField = clazz.getDeclaredField(parentId);

        } catch (NoSuchFieldException e) {

            parentField = clazz.getSuperclass().getDeclaredField(parentId);

        }

        Field childrenField;

        try {

            childrenField = clazz.getDeclaredField(children);

        } catch (NoSuchFieldException e) {

            childrenField = clazz.getSuperclass().getDeclaredField(children);

        }

        idField.setAccessible(true);

        parentField.setAccessible(true);

        childrenField.setAccessible(true);

        List<T> tree = new ArrayList<>();

        for (T t : list) {

            Object o = parentField.get(t);

            if (o == null) {

                tree.add(findChild(t, list, idField, parentField, childrenField));

            } else {

                boolean flag = true;

                for (T t1 : list) {

                    Object o1 = idField.get(t1);

                    if (o.equals(o1)) {

                        flag = false;

                        break;

                    }

                }

                if (flag) {

                    tree.add(findChild(t, list, idField, parentField, childrenField));

                }

            }

        }

        return tree;

    }

static <T> T findChild( T node, List<T> list, Field id, Field parent, Field children ) throws IllegalAccessException{

        for (T n : list) {

            if (parent.get(n).equals(id.get(node))) {

                if (children.get(node) == null) {

                    children.set(node, new ArrayList<T>());

                }

                List list1 = (List) children.get(node);

                list1.add(findChild(n, list, id, parent, children));

            }

        }

        return node;

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值