Application of list to Tree Transformation in Java
This article mainly explains "the application of list tree transformation in Java". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the application of list tree transformation in Java".
Public class TreeNode {private String id; private String parentId; private String name; private List children; public TreeNode (String id, String name, String parentId) {this.id = id; this.parentId = parentId; this.name = name;} public TreeNode (String id, String name, TreeNode parent) {this.id = id; this.parentId = parent.getId (); this.name = name } public String getParentId () {return parentId;} public void setParentId (String parentId) {this.parentId = parentId;} public String getName () {return name;} public void setName (String name) {this.name = name;} public String getId () {return id;} public void setId (String id) {this.id = id } public List getChildren () {return children;} public void setChildren (List children) {this.children = children } @ Override public String toString () {return "TreeNode {" + "id='" + id +'\'+ ", parentId='" + parentId +'\'+ ", name='" + name +'\'+ ", children=" + children +'}' }} public class TreeBuilder {/ * two-layer Loop implementation Tree Construction * @ param treeNodes list of incoming tree nodes * @ return * / public static List bulid (List treeNodes) {List trees = new ArrayList (); for (TreeNode treeNode: treeNodes) {if ("0" .equals (treeNode.getParentId () {trees.add (treeNode) } for (TreeNode it: treeNodes) {if (it.getParentId () = = treeNode.getId ()) {if (treeNode.getChildren () = = null) {treeNode.setChildren (new ArrayList ());} treeNode.getChildren () .add (it) } return trees;}} Thank you for your reading, the above is the content of "the application of list tree conversion in Java". After the study of this article, I believe you have a deeper understanding of the application of list tree conversion in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!