Java 构建树形结构方法 - 基于 BaseTreeNode 实现
/**
-
构建树形结构的方法
-
@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()); }
原文地址: https://www.cveoy.top/t/topic/qx2v 著作权归作者所有。请勿转载和采集!