根据子类查询父类

1.如果改子类有父类,则把父类加入子类的父ID中

 private static List<MentVo> breadthParentFirst(List<MentVo> rootTree) {
        if (CollectionUtils.isEmpty(rootTree)) {
            return Lists.newArrayList();
        }
        List<MentVo> mentVoList=Lists.newArrayList();
        rootTree.forEach((root) -> {
            Queue<MentVo> queue = new LinkedList();
            queue.offer(root);
            while (!queue.isEmpty()){
                MentVo  tree = (MentVo) queue.poll();
                List<MentVo> parent = tree.getParent();
                if (!CollectionUtils.isEmpty(parent)) {
                    Iterator var5 = parent.iterator();
                    while (var5.hasNext()) {
                        MentVo child = (MentVo) var5.next();
                        queue.offer(child);
                    }
                }
                mentVoList.add(tree);
            }
        });
        return mentVoList;
}

2.如果父类id不为空,则查询该子类的父类。

 Map<String, List<MentVo>> addListMap = departmentList.stream()
                .filter(depart -> Objects.nonNull(depart.getId())).collect(Collectors.groupingBy(MentVo::getId));
        departmentList.forEach((item) -> {
            if (!CollectionUtils.isEmpty(addListMap.get(item.getParentid()))) {
                item.setParent(addListMap.get(item.getParentid()));
            }
        });
        departmentList.forEach((item) -> {
            item.setLevel(breadthParentFirst(item.getParent()).size());
        });
        departmentList = departmentList.stream().sorted((x, y) -> {
            return x.getLevel().compareTo(y.getLevel());
        }).collect(Collectors.toList());

 

posted @ 2022-09-13 10:26  sowler  阅读(88)  评论(0编辑  收藏  举报