List轉為Tree的方法__JAVA學習

來源:互聯網
上載者:User
package com.xicheng.test;import java.util.List;/** * 2018年3月26日上午7:32:37 * 樹節點實體類 */public class TreeNode {private String id;private String parentId;private String name;private List<TreeNode> children;public String getId() {return id;}public void setId(String id) {this.id = id;}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 List<TreeNode> getChildren() {return children;}public void setChildren(List<TreeNode> children) {this.children = children;}@Overridepublic String toString() {return "TreeNode [id=" + id + ", parentId=" + parentId + ", name=" + name + ", children=" + children + "]";}public TreeNode() {}public TreeNode(String id, String name, String parentId) {this.id = id;this.parentId = parentId;this.name = name;}}

package com.xicheng.test;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class List2Tree {public static void main(String[] args) {TreeNode t1 = new TreeNode("1","p1","0");TreeNode t2 = new TreeNode("2","p2","0");TreeNode t3 = new TreeNode("3","s11","1");TreeNode t4 = new TreeNode("4","s12","1");TreeNode t5 = new TreeNode("5","s21","2");TreeNode t6 = new TreeNode("6","s22","2");TreeNode t7 = new TreeNode("7","s23","2");List<TreeNode> treeList = new ArrayList<>();Collections.addAll(treeList, t1,t2,t3,t4,t5,t6,t7);System.out.println(toTree01(treeList));System.out.println(toTree02(treeList));}/** * 方式一:使用兩層迴圈實現 * @param list * @return */public static List<TreeNode> toTree01(List<TreeNode> treeList) {List<TreeNode> retList = new ArrayList<>();for (TreeNode parent : treeList) {if ("0".equals(parent.getParentId())) {retList.add(parent);}for (TreeNode child : treeList) {if (child.getParentId() == parent.getId()) {if (parent.getChildren() == null) {parent.setChildren(new ArrayList<>());}parent.getChildren().add(child);}}}return retList;}/** * 方式二:使用遞迴實現 * @param treeList * @return */public static List<TreeNode> toTree02(List<TreeNode> treeList) {List<TreeNode> retList = new ArrayList<>();for (TreeNode parent : treeList) {if ("0".equals(parent.getParentId())) {retList.add(findChildren(parent, treeList));}}return retList;}private static TreeNode findChildren(TreeNode parent, List<TreeNode> treeList) {for (TreeNode child : treeList) {if (parent.getId().equals(child.getParentId())) {if (parent.getChildren() == null) {parent.setChildren(new ArrayList<>());}parent.getChildren().add(findChildren(child, treeList));}}return parent;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.