1、利用場景
組織機構樹,通常會有組織機構表,其中有code(代碼),pcode(上級代碼),name(組織名稱)等字段
2、構造數據(以下數據並不是組織機構數據,而純屬本人胡編亂造的數據)
1 List<Tree<Test>> trees = new ArrayList<Tree<Test>>(); 2 tests.add(new Test("0", "", "關於本人")); 3 tests.add(new Test("1", "0", "技術學習")); 4 tests.add(new Test("2", "0", "興趣")); 5 tests.add(new Test("3", "1", "JAVA")); 6 tests.add(new Test("4", "1", "oracle")); 7 tests.add(new Test("5", "1", "spring")); 8 tests.add(new Test("6", "1", "springmvc")); 9 tests.add(new Test("7", "1", "fastdfs")); 10 tests.add(new Test("8", "1", "linux")); 11 tests.add(new Test("9", "2", "騎行")); 12 tests.add(new Test("10", "2", "吃喝玩樂")); 13 tests.add(new Test("11", "2", "學習")); 14 tests.add(new Test("12", "3", "String")); 15 tests.add(new Test("13", "4", "sql")); 16 tests.add(new Test("14", "5", "ioc")); 17 tests.add(new Test("15", "5", "aop")); 18 tests.add(new Test("16", "1", "等等")); 19 tests.add(new Test("17", "2", "等等")); 20 tests.add(new Test("18", "3", "等等")); 21 tests.add(new Test("19", "4", "等等")); 22 tests.add(new Test("20", "5", "等等"));
3、源碼
Tree.java
1 package pers.kangxu.datautils.bean.tree; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Map; 6 7 import com.alibaba.fastjson.JSON; 8 9 /** 10 * tree TODO <br> 11 * 12 * @author kangxu2 2017-1-7 13 * 14 */ 15 public class Tree<T> { 16 /** 17 * 節點ID 18 */ 19 private String id; 20 /** 21 * 顯示節點文本 22 */ 23 private String text; 24 /** 25 * 節點狀態,open closed 26 */ 27 private String state = "open"; 28 /** 29 * 節點是否被選中 true false 30 */ 31 private boolean checked = false; 32 /** 33 * 節點屬性 34 */ 35 private List<Map<String, Object>> attributes; 36 /** 37 * 節點的子節點 38 */ 39 private List<Tree<T>> children = new ArrayList<Tree<T>>(); 40 41 /** 42 * 父ID 43 */ 44 private String parentId; 45 /** 46 * 是否有父節點 47 */ 48 private boolean isParent = false; 49 /** 50 * 是否有子節點 51 */ 52 private boolean isChildren = false; 53 54 public String getId() { 55 return id; 56 } 57 58 public void setId(String id) { 59 this.id = id; 60 } 61 62 public String getText() { 63 return text; 64 } 65 66 public void setText(String text) { 67 this.text = text; 68 } 69 70 public String getState() { 71 return state; 72 } 73 74 public void setState(String state) { 75 this.state = state; 76 } 77 78 public boolean isChecked() { 79 return checked; 80 } 81 82 public void setChecked(boolean checked) { 83 this.checked = checked; 84 } 85 86 public List<Map<String, Object>> getAttributes() { 87 return attributes; 88 } 89 90 public void setAttributes(List<Map<String, Object>> attributes) { 91 this.attributes = attributes; 92 } 93 94 public List<Tree<T>> getChildren() { 95 return children; 96 } 97 98 public void setChildren(List<Tree<T>> children) { 99 this.children = children; 100 } 101 102 public boolean isParent() { 103 return isParent; 104 } 105 106 public void setParent(boolean isParent) { 107 this.isParent = isParent; 108 } 109 110 public boolean isChildren() { 111 return isChildren; 112 } 113 114 public void setChildren(boolean isChildren) { 115 this.isChildren = isChildren; 116 } 117 118 public String getParentId() { 119 return parentId; 120 } 121 122 public void setParentId(String parentId) { 123 this.parentId = parentId; 124 } 125 126 public Tree(String id, String text, String state, boolean checked, 127 List<Map<String, Object>> attributes, List<Tree<T>> children, 128 boolean isParent, boolean isChildren, String parentID) { 129 super(); 130 this.id = id; 131 this.text = text; 132 this.state = state; 133 this.checked = checked; 134 this.attributes = attributes; 135 this.children = children; 136 this.isParent = isParent; 137 this.isChildren = isChildren; 138 this.parentId = parentID; 139 } 140 141 public Tree() { 142 super(); 143 } 144 145 @Override 146 public String toString() { 147 148 return JSON.toJSONString(this); 149 } 150 151 }
BuildTree.java
1 package pers.kangxu.datautils.common.tree; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import pers.kangxu.datautils.bean.tree.Tree; 7 8 /** 9 * 構建tree 10 * TODO 11 * <br> 12 * @author kangxu2 2017-1-7 13 * 14 */ 15 public class BuildTree { 16 17 /** 18 * 19 * TODO 20 * <br> 21 * @author kangxu2 2017-1-7 22 * 23 * @param nodes 24 * @return 25 */ 26 public static <T> Tree<T> build(List<Tree<T>> nodes) { 27 28 if(nodes == null){ 29 return null; 30 } 31 List<Tree<T>> topNodes = new ArrayList<Tree<T>>(); 32 33 for (Tree<T> children : nodes) { 34 35 String pid = children.getParentId(); 36 if (pid == null || "".equals(pid)) { 37 topNodes.add(children); 38 39 continue; 40 } 41 42 for (Tree<T> parent : nodes) { 43 String id = parent.getId(); 44 if (id != null && id.equals(pid)) { 45 parent.getChildren().add(children); 46 children.setParent(true); 47 parent.setChildren(true); 48 49 continue; 50 } 51 } 52 53 } 54 55 Tree<T> root = new Tree<T>(); 56 if (topNodes.size() == 0) { 57 root = topNodes.get(0); 58 } else { 59 root.setId("-1"); 60 root.setParentId(""); 61 root.setParent(false); 62 root.setChildren(true); 63 root.setChecked(true); 64 root.setChildren(topNodes); 65 root.setText("頂級節點"); 66 67 } 68 69 return root; 70 } 71 72 }
BuildTreeTester.java
1 package pers.kangxu.datautils.test; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import pers.kangxu.datautils.bean.tree.Tree; 7 import pers.kangxu.datautils.common.tree.BuildTree; 8 9 public class BuildTreeTester { 10 11 public static void main(String[] args) { 12 13 14 List<Tree<Test>> trees = new ArrayList<Tree<Test>>(); 15 List<Test> tests = new ArrayList<Test>(); 16 tests.add(new Test("0", "", "關於本人")); 17 tests.add(new Test("1", "0", "技術學習")); 18 tests.add(new Test("2", "0", "興趣")); 19 tests.add(new Test("3", "1", "JAVA")); 20 tests.add(new Test("4", "1", "oracle")); 21 tests.add(new Test("5", "1", "spring")); 22 tests.add(new Test("6", "1", "springmvc")); 23 tests.add(new Test("7", "1", "fastdfs")); 24 tests.add(new Test("8", "1", "linux")); 25 tests.add(new Test("9", "2", "騎行")); 26 tests.add(new Test("10", "2", "吃喝玩樂")); 27 tests.add(new Test("11", "2", "學習")); 28 tests.add(new Test("12", "3", "String")); 29 tests.add(new Test("13", "4", "sql")); 30 tests.add(new Test("14", "5", "ioc")); 31 tests.add(new Test("15", "5", "aop")); 32 tests.add(new Test("16", "1", "等等")); 33 tests.add(new Test("17", "2", "等等")); 34 tests.add(new Test("18", "3", "等等")); 35 tests.add(new Test("19", "4", "等等")); 36 tests.add(new Test("20", "5", "等等")); 37 38 for (Test test : tests) { 39 Tree<Test> tree = new Tree<Test>(); 40 tree.setId(test.getId()); 41 tree.setParentId(test.getPid()); 42 tree.setText(test.getText()); 43 44 trees.add(tree); 45 } 46 47 Tree<Test> t = BuildTree.build(trees); 48 System.out.println(t); 49 } 50 } 51 52 class Test { 53 54 private String id; 55 private String pid; 56 private String text; 57 58 public String getId() { 59 return id; 60 } 61 62 public void setId(String id) { 63 this.id = id; 64 } 65 66 public String getPid() { 67 return pid; 68 } 69 70 public void setPid(String pid) { 71 this.pid = pid; 72 } 73 74 public String getText() { 75 return text; 76 } 77 78 public void setText(String text) { 79 this.text = text; 80 } 81 82 public Test(String id, String pid, String text) { 83 super(); 84 this.id = id; 85 this.pid = pid; 86 this.text = text; 87 } 88 89 public Test() { 90 super(); 91 } 92 93 @Override 94 public String toString() { 95 return "Test [id=" + id + ", pid=" + pid + ", text=" + text + "]"; 96 } 97 98 }
4、運行結果
JSON數據:
{ "checked": true, "children": [ { "checked": false, "children": [ { "checked": false, "children": [ { "checked": false, "children": [ { "checked": false, "children": [], "id": "12", "parent": true, "parentId": "3", "state": "open", "text": "String" }, { "checked": false, "children": [], "id": "18", "parent": true, "parentId": "3", "state": "open", "text": "等等" } ], "id": "3", "parent": true, "parentId": "1", "state": "open", "text": "JAVA" }, { "checked": false, "children": [ { "checked": false, "children": [], "id": "13", "parent": true, "parentId": "4", "state": "open", "text": "sql" }, { "checked": false, "children": [], "id": "19", "parent": true, "parentId": "4", "state": "open", "text": "等等" } ], "id": "4", "parent": true, "parentId": "1", "state": "open", "text": "oracle" }, { "checked": false, "children": [ { "checked": false, "children": [], "id": "14", "parent": true, "parentId": "5", "state": "open", "text": "ioc" }, { "checked": false, "children": [], "id": "15", "parent": true, "parentId": "5", "state": "open", "text": "aop" }, { "checked": false, "children": [], "id": "20", "parent": true, "parentId": "5", "state": "open", "text": "等等" } ], "id": "5", "parent": true, "parentId": "1", "state": "open", "text": "spring" }, { "checked": false, "children": [], "id": "6", "parent": true, "parentId": "1", "state": "open", "text": "springmvc" }, { "checked": false, "children": [], "id": "7", "parent": true, "parentId": "1", "state": "open", "text": "fastdfs" }, { "checked": false, "children": [], "id": "8", "parent": true, "parentId": "1", "state": "open", "text": "linux" }, { "checked": false, "children": [], "id": "16", "parent": true, "parentId": "1", "state": "open", "text": "等等" } ], "id": "1", "parent": true, "parentId": "0", "state": "open", "text": "技術學習" }, { "checked": false, "children": [ { "checked": false, "children": [], "id": "9", "parent": true, "parentId": "2", "state": "open", "text": "騎行" }, { "checked": false, "children": [], "id": "10", "parent": true, "parentId": "2", "state": "open", "text": "吃喝玩樂" }, { "checked": false, "children": [], "id": "11", "parent": true, "parentId": "2", "state": "open", "text": "學習" }, { "checked": false, "children": [], "id": "17", "parent": true, "parentId": "2", "state": "open", "text": "等等" } ], "id": "2", "parent": true, "parentId": "0", "state": "open", "text": "興趣" } ], "id": "0", "parent": false, "parentId": "", "state": "open", "text": "關於本人" } ], "id": "-1", "parent": false, "parentId": "", "state": "open", "text": "頂級節點" }