Java List 生成 树(增强版)
程序员文章站
2022-05-09 18:35:28
...
Java List 生成 树:http://ysj5125094.iteye.com/blog/2283159
maven pom.xml
<dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency>
TreeBuilder.java
package com.yusj.util.tree; import java.util.ArrayList; import java.util.List; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import com.alibaba.fastjson.JSON; import lac.framework.support.dictionary.domain.Dictionary; public class TreeBuilder { @SuppressWarnings("unchecked") public List<? extends Node> buildListToTree(List<? extends Node> dirs) { List<Node> roots = findRoots(dirs); List<Node> notRoots = (List<Node>) CollectionUtils.subtract(dirs, roots); for (Node root : roots) { root.setChildren(findChildren(root, notRoots)); } return roots; } private List<Node> findRoots(List<? extends Node> allNodes) { List<Node> results = new ArrayList<Node>(); for (Node node : allNodes) { boolean isRoot = true; for (Node comparedOne : allNodes) { if (StringUtils.isNotBlank(node.getParentId()) && node.getParentId().equals(comparedOne.getId())) { isRoot = false; break; } } if (isRoot) { node.setLevel(0); results.add(node); node.setRootId(node.getId()); } } return results; } @SuppressWarnings("unchecked") private List<Node> findChildren(Node root, List<Node> allNodes) { List<Node> children = new ArrayList<Node>(); for (Node comparedOne : allNodes) { if (StringUtils.isNotBlank(comparedOne.getParentId()) && comparedOne.getParentId().equals(root.getId())) { comparedOne.setParent(root); comparedOne.setLevel(root.getLevel() + 1); children.add(comparedOne); } } List<Node> notChildren = (List<Node>) CollectionUtils.subtract(allNodes, children); for (Node child : children) { List<Node> tmpChildren = findChildren(child, notChildren); if (tmpChildren == null || tmpChildren.size() < 1) { child.setLeaf(true); } else { child.setLeaf(false); } child.setChildren(tmpChildren); } return children; } private List<Node> getLeafChildren(List<Node> resultList, List<Node> children) { for (Node node : children) { if (node.isLeaf()) { resultList.add(node); } else { getLeafChildren(resultList, node.getChildren()); } } return resultList; } @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { TreeBuilder tb = new TreeBuilder(); List<Node> allNodes = new ArrayList<Node>(); allNodes.add(new Dictionary("1", "0", "001", "节点001", 0)); allNodes.add(new Dictionary("2", "0", "002", "节点002", 0)); allNodes.add(new Dictionary("3", "0", "003", "节点003", 0)); allNodes.add(new Dictionary("4", "1", "004", "节点004", 0)); allNodes.add(new Dictionary("5", "1", "005", "节点005", 0)); allNodes.add(new Dictionary("6", "1", "006", "节点006", 0)); allNodes.add(new Dictionary("7", "4", "007", "节点007", 0)); allNodes.add(new Dictionary("8", "4", "008", "节点008", 0)); allNodes.add(new Dictionary("9", "5", "009", "节点009", 0)); allNodes.add(new Dictionary("10", "5", "010", "节点010", 0)); allNodes.add(new Dictionary("11", "7", "011", "节点011", 0)); allNodes.add(new Dictionary("12", "7", "012", "节点012", 0)); // 显示所有节点 List<Node> roots = (List<Node>) tb.buildListToTree(allNodes); for (Node n : roots) { System.out.println(JSON.toJSONString(n)); } // 查找所有子节点 List<Node> children = tb.findChildren(new Dictionary("1", "0"), allNodes); for (Node n : children) { System.out.println(JSON.toJSONString(n)); } // 查找所有叶子节点 System.out.println("------------------"); List<Node> resultList = tb.getLeafChildren(new ArrayList<Node>(), children); for (Node n : resultList) { System.out.println(JSON.toJSONString(n)); } } }
Node.java(待转换的bean必须继承该类)
package com.yusj.util.tree; import java.util.List; import com.fasterxml.jackson.annotation.JsonIgnore; import lac.framework.core.entity.IdEntity; public class Node extends IdEntity{ /** * */ private static final long serialVersionUID = 8875995344582620331L; private String parentId; private Node parent; private List<Node> children; private int level; private String rootId; private boolean leaf; public Node(){} public Node(String id, String parentId){ this.setId(id); this.parentId = parentId; } public String getParentId() { return parentId; } public void setParentId(String parentId) { this.parentId = parentId; } @JsonIgnore public Node getParent() { return parent; } public void setParent(Node parent) { this.parent = parent; } public List<Node> getChildren() { return children; } public void setChildren(List<Node> children) { this.children = children; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } public String getRootId() { return rootId; } public void setRootId(String rootId) { this.rootId = rootId; } public boolean isLeaf() { return leaf; } public void setLeaf(boolean leaf) { this.leaf = leaf; } }
Dictionary.java
package com.yusj.support.dictionary.domain; import com.yusj.util.tree.Node; public class Dictionary extends Node{ /** * */ private static final long serialVersionUID = 8875995344582620331L; private String code; private String label; private Integer sort; public Dictionary(){} public Dictionary(String id, String parentId){ super(id, parentId); } public Dictionary(String id, String parentId, String code, String label, Integer sort){ super(id, parentId); this.code = code; this.label = label; this.sort = sort; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public Integer getSort() { return sort; } public void setSort(Integer sort) { this.sort = sort; } }
推荐阅读
-
最小生成树的java实现
-
java只需一个查询生成xml树传至flex绑定tree
-
Java对学生成绩排序——通过list.sort()对list进行排序
-
Java 设计一个Hero二叉树,HeroNode. 可以向这个英雄二叉树插入不同的Hero对象,并且按照Hero的血量升排序。 随机生成10个Hero对象,每个Hero对象都有不同的血量值,插
-
Java List 生成 树(转)
-
Java List 生成 树(转)
-
Java求最小生成树的两种算法详解
-
贪婪算法在求解最小生成树中的应用(JAVA)--Prim算法
-
Java List 生成 树(增强版)
-
贪心算法:Dijkstra最短路径,Prim最小生成树,Kruskal最小生成树(Java实现)