【8.3.3】整理树排序代码

master^2
stylefeng 2024-12-11 15:43:42 +08:00
parent b7714ba53a
commit 0588fcfb4e
1 changed files with 11 additions and 5 deletions

View File

@ -57,7 +57,7 @@ public class SortedTreeBuildFactory<T extends AbstractSortedTreeNode<T>> extends
public List<T> doTreeBuild(List<T> nodes) { public List<T> doTreeBuild(List<T> nodes) {
// 先对列表进行排序 // 先对列表进行排序
nodes.sort(Comparator.comparing(m -> Optional.ofNullable((m).getSort()).orElse(new BigDecimal(Integer.MAX_VALUE)))); nodes.sort(Comparator.comparing(itemNode -> Optional.ofNullable((itemNode).getSort()).orElse(new BigDecimal(Integer.MAX_VALUE))));
// 将每个节点构造一个子树 // 将每个节点构造一个子树
for (T treeNode : nodes) { for (T treeNode : nodes) {
@ -76,7 +76,7 @@ public class SortedTreeBuildFactory<T extends AbstractSortedTreeNode<T>> extends
List<T> nodeSubLists = getSubChildsLevelOne(totalNodes, node); List<T> nodeSubLists = getSubChildsLevelOne(totalNodes, node);
// 对子节点进行排序 // 对子节点进行排序
nodeSubLists.sort(Comparator.comparing(m -> Optional.ofNullable((m).getSort()).orElse(new BigDecimal(Integer.MAX_VALUE)))); nodeSubLists.sort(Comparator.comparing(itemNode -> Optional.ofNullable((itemNode).getSort()).orElse(new BigDecimal(Integer.MAX_VALUE))));
if (!nodeSubLists.isEmpty()) { if (!nodeSubLists.isEmpty()) {
for (T nodeSubList : nodeSubLists) { for (T nodeSubList : nodeSubLists) {
@ -88,13 +88,19 @@ public class SortedTreeBuildFactory<T extends AbstractSortedTreeNode<T>> extends
node.setChildrenNodes(childNodeLists); node.setChildrenNodes(childNodeLists);
} }
/**
* nodeid
*
* @author fengshuonan
* @since 2024/12/11 15:35
*/
private List<T> findTops(List<T> nodes) { private List<T> findTops(List<T> nodes) {
List<T> parents = nodes.stream().filter((a) -> null != a.getNodeParentId()).collect(Collectors.toList()); List<T> totalParentList = nodes.stream().filter((itemNode) -> null != itemNode.getNodeParentId()).collect(Collectors.toList());
List<String> entityIds = nodes.stream().map(AbstractSortedTreeNode::getNodeId).collect(Collectors.toList()); List<String> totalNodeIdList = nodes.stream().map(AbstractSortedTreeNode::getNodeId).collect(Collectors.toList());
return parents.stream().filter((a) -> !entityIds.contains(a.getNodeParentId())).distinct().collect(Collectors.toList()); return totalParentList.stream().filter((item) -> !totalNodeIdList.contains(item.getNodeParentId())).distinct().collect(Collectors.toList());
} }
} }