/**

  • 构建树形结构的方法

  • @param nodes 节点列表

  • @param 节点类型,必须是BaseTreeNode的子类

  • @return 构建好的树形结构 */ public static <T extends BaseTreeNode> List buildTree(List nodes) {

    // 构建父节点ID到子节点列表的映射关系 final Map<String, List> childrenNodeGroup = CollectionUtils.emptyIfNull(nodes).stream() .filter(l -> !l.checkIsRoot()) .collect(Collectors.groupingBy(BaseTreeNode::getParentId, Collectors.toList()));

    return CollectionUtils.emptyIfNull(nodes).stream() .peek(l -> l.setChildrens(MapUtils.getObject(childrenNodeGroup, l.getId()))) .filter(T::checkIsRoot) .collect(Collectors.toList());

添加注释: public static T extends BaseTreeNodeT ListT buildTreeListT nodes 构建父节点ID到子节点列表的映射关系 final MapString ListT childrenNodeGroup = CollectionUtilsemptyIfNullnodesstream fil

原文地址: https://www.cveoy.top/t/topic/iR2Q 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录