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());
}
标签:java,stream,子类,List,collect,树形,单表,getChildCategoryList,categories 来源: https://www.cnblogs.com/chcbk/p/16498184.html