java 单表树形组装

List<PdTechnologyCommunityComment> pdTechnologyCommunityComments = pdTechnologyCommunityCommentMapper.listAllByPdTechnologyCommunityComment(var);
需要组装的数据
List<PdTechnologyCommunityComment> collect = pdTechnologyCommunityComments.stream().filter(o -> o.getParentCid() == 0)
            // 给每个一级分类加子分类
            .peek(o -> o.setChildrens(getChildCategoryList(o, categories)))
            // 排序
            .sorted(Comparator.comparingInt(CategoryEntity::getSort))
            // 收集
            .collect(Collectors.toList());



// 根据当前分类 找出子类, 并通过递归找出子类的子类
private List<PdTechnologyCommunityComment> getChildCategoryList(PdTechnologyCommunityComment currMenu, List<PdTechnologyCommunityComment> categories) {

return categories.stream().filter(o -> o.getParentCid().equals(currMenu.getCatId()))
            .peek(o -> o.setChildrens(getChildCategoryList(o, categories)))
            .sorted(Comparator.comparingInt(CategoryEntity::getSort))
            .collect(Collectors.toList());

}
posted @ 2022-07-20 15:25  才没有木头  阅读(84)  评论(0)    收藏  举报