java中收集父节点下的所有子节点
递归算法,收集父节点下所有的子节点数据
/**
* 收集子节点ID
*
* @param categoryId 类别id
* @return {@link Set}<{@link Long}>
*/
public Set<Long> selectCategoryAndChildrenById(Long categoryId) {
Set<Long> categorySet = Sets.newHashSet();
findChildCategory(categorySet, categoryId);
Set<Long> categoryIdList = Sets.newHashSet();
if (Func.isNotEmpty(categoryId)) {
for (Long id : categorySet) {
categoryIdList.add(id);
}
}
return categoryIdList;
}
/**
* 递归算法,算出子节点
*
* @param categorySet 类别设置
* @param categoryId 类别id
* @return {@link Set}<{@link Long}>
*/
private Set<Long> findChildCategory(Set<Long> categorySet, Long categoryId) {
ConquasInspectionType inspectionType = this.getById(categoryId);
if (Func.isNotEmpty(inspectionType)) {
categorySet.add(inspectionType.getId());
}
QueryWrapper<ConquasInspectionType> conquasInspectionTypeQueryWrapper = new QueryWrapper<>();
conquasInspectionTypeQueryWrapper.eq("parent_id", categoryId);
List<ConquasInspectionType> inspectionTypeList = this.list(conquasInspectionTypeQueryWrapper);
for (ConquasInspectionType conquasInspectionType : inspectionTypeList) {
findChildCategory(categorySet, conquasInspectionType.getId());
}
return categorySet;
}
该博客介绍了一种在Java中使用递归算法收集指定父节点下所有子节点ID的方法。通过调用`selectCategoryAndChildrenById`方法,可以获取到以类别ID为根的所有子类别ID的集合。`findChildCategory`方法用于递归查找并添加子节点到集合中,直到遍历完所有子节点。涉及到的数据实体为`ConquasInspectionType`,并且查询条件基于`parent_id`字段。
4091

被折叠的 条评论
为什么被折叠?



