1. Skenario pemanfaatan
Pohon struktur organisasi, biasanya ada tabel struktur organisasi, termasuk bidang seperti kode (kode), PCODE (kode atas), nama (nama organisasi)
2. Data Konstruksi (data berikut bukan data organisasi, tetapi murni data yang saya buat)
Daftar <Tree <Test>> Pohon = Daftar Array baru <Tree <Test>> (); tests.add (tes baru ("0", "", "tentang saya")); tests.add (tes baru ("1", "0", "pembelajaran teknis")); tests.add (tes baru ("2", "0", "minat")); tests.add "Java")); tests.add (tes baru ("4", "1", "oracle")); tests.add (tes baru ("5", "1", "spring")); tests.add (tes baru ("6", "1", "springmvc")); tests.add (tes baru ("7", "" 1 "", "1", "" 1 "," "1", "" 1 "," 1, "" 1, "" 1, "", "7", "" 1, "", "7", "1", "1", "1", "1", "1", "1", "1," 1, "1," 1, "1," 1, "1," 1, "1," 1, "1," 1, "1," 1 "," 1, "1", "1," 1, "7", "7" "," 7 "", "7" "," 7 "", "7" "," 7 "", "7" "," "1", "linux")); tests.add (tes baru ("9", "2", "bersepeda")); tests.add (tes baru ("10", "2", "Eat, Drink, Play")); tests.add (tes baru ("11", "2", "learning"); tes. "4", "sql")); tests.add (tes baru ("14", "5", "ioc")); tests.add (tes baru ("15", "5", "aop")); tests.add (tes baru ("" "," "" ")); tests.add (test baru (" 17 "", "" "")); tests.add (uji baru ("17", "", "" "); "dll")); tests.add (tes baru ("19", "4", "dll")); tests.add (tes baru ("20", "5", "dll")); tests.add (tes baru ("20", "5", "dll")); tests.add (tes baru ("20", "5", "" ""); " "dll")); tests.add (tes baru ("19", "4", "dll")); tests.add (tes baru ("20", "5", "dll")); tests.add (tes baru ("20", "5", "dll"));3. Kode Sumber
Pohon.java
Paket Pers.kangxu.datautils.bean.tree; import java.util.arraylist; impor java.util.list; pengenal; / *** Tampilkan teks simpul*/ teks string pribadi; / ** * status simpul, buka tertutup */ private string state = "buka"; / ** * Apakah node dipilih benar -benar salah */ boolean private checked = false; / *** atribut simpul*/ daftar pribadi <peta <string, objek >> atribut; / *** Anak -anak dari simpul*/ daftar pribadi <Tree <T>> Anak -anak = Daftar Array baru <Tree <T>> (); / ** * ID induk */ Private String ParentID; / *** Apakah ada node induk*/ private boolean isParent = false; / *** Apakah ada simpul anak*/ boolean private ischildren = false; string publik getId () {return id; } public void setId (string id) {this.id = id; } public string getText () {return text; } public void setText (string text) {this.text = text; } public String getState () {return state; } public void setState (state string) {this.state = state; } public boolean isChecked () {return checked; } public void setChecked (boolean diperiksa) {this.checked = checked; } Daftar publik <peta <string, objek >> getAttributes () {return atributes; } public void setAttributes (daftar <peta <string, object >> atribut) {this.attributes = atribut; } Daftar Publik <Tree <T>> getChildren () {Return Children; } public void setChildren (daftar <Tree <T>> anak -anak) {this.children = anak -anak; } public boolean isParent () {return isParent; } public void setParent (boolean isParent) {this.isparent = isParent; } public boolean ischildren () {return ischildren; } public void setChildren (boolean ischildren) {this.ischildren = ischildren; } public string getParEndId () {return parentId; } public void setParentId (String ParentId) {this.parentId = parentId; } Pohon publik (ID String, Teks String, Status String, Dicari Boolean, Daftar <Map <String, Object >> Atribut, Daftar <te Tree <t>> Anak -anak, Boolean Isparent, Boolean Ischildren, String ParentID) {super (); this.id = id; this.text = teks; this.state = state; this.Checked = diperiksa; this.attributes = atribut; this.children = anak -anak; this.isparent = isParent; this.ischildren = ischildren; this.parentId = parentId; } Pohon publik () {super (); } @Override public string toString () {return json.toJsonstring (this); }}Buildtree.java
Paket Pers.kangxu.datautils.common.tree; import java.util.arraylist; import java.util.list; pers.kangxu.datautils.bean.tree.tree;/** * TOGLE * TODE */<br> * @author Kangxu2 2017-1- ** * TODE */<br> * @author kangxu2 2017-1- ** * @Author kangxu2 2017-1-7 * * @param node * @return */ public static <T> Tree <T> build (Daftar <Tree <T>> node) {if (node == null) {return null; } Daftar <Tree <T>> TopNodes = ArrayList baru <Tree <T>> (); untuk (pohon <T> anak -anak: node) {string pid = children.getParentId (); if (pid == null || "" .Equals (pid)) {topnodes.add (anak -anak); melanjutkan; } untuk (pohon <T> induk: node) {string id = parent.getId (); if (id! = null && id.equals (pid)) {parent.getchildren (). add (anak -anak); anak -anak.setParent (true); Parent.setchildren (true); melanjutkan; }}} Tree <T> root = Tree baru <T> (); if (topnodes.size () == 0) {root = topnodes.get (0); } else {root.setId ("-1"); root.setParentId (""); root.setParent (false); root.setchildren (true); root.setchecked (true); root.setchildren (topnodes); root.setText ("Node tingkat atas"); } return root; }}Buildtreetester.java
Paket Pers.kangxu.datautils.test; import java.util.arraylist; impor java.util.list; impor pers.kangxu.datautils.bean.tree.tree; public class {datautils.common.tree.builde; public class {datautils.common.tree.builde; Public class {datautils.common.tree.builde; Public class {datautils.common.tree.builde; Public class {datautils.common.tree Daftar <Tree <est>> Trees = Daftar Arraylist Baru <Tree <Test> (); Daftar <Test> tes = ArrayList baru <Test> (); tests.add (tes baru ("0", "", "tentang saya")); tests.add (tes baru ("1", "0", "pembelajaran teknis")); tests.add (tes baru ("2", "0", "minat")); tests.add (tes baru ("3", "1", "java")); tests.add (tes baru ("4", "1", "oracle")); tests.add (tes baru ("5", "1", "spring"))); tests.add (tes baru ("6", "1", "springmvc")); tests.add (tes baru ("7", "1", "fastdfs")); tests.add (tes baru ("8", "1", "linux")); tests.add (tes baru ("9", "2", "bersepeda")); tests.add (tes baru ("10", "2", "makan, minum, dan bersenang -senang")); tests.add (tes baru ("11", "2", "belajar")); tests.add (tes baru ("12", "3", "string")); tests.add (tes baru ("13", "4", "sql")); tests.add (tes baru ("14", "5", "IOC")); tests.add (tes baru ("15", "5", "AOP")); tests.add (tes baru ("16", "1", "dll")); tests.add (tes baru ("17", "2", "dll")); tests.add (tes baru ("18", "3", "dll")); tests.add (tes baru ("19", "4", "dll")); tests.add (tes baru ("20", "5", "dll")); untuk (tes tes: tes) {pohon <pree <test> pohon = pohon baru <dest> (); tree.setid (test.getId ()); Tree.setParentId (test.getPid ()); Tree.setText (test.getText ()); pohon.add (pohon); } Tree <test> t = buildtree.build (pohon); System.out.println (t); }} class test {private string id; Private String PID; teks string pribadi; string publik getId () {return id; } public void setId (string id) {this.id = id; } public string getPid () {return pid; } public void setPid (String pid) {this.pid = pid; } public string getText () {return text; } public void setText (string text) {this.text = text; } tes publik (ID String, String PID, Teks String) {super (); this.id = id; this.pid = pid; this.text = teks; } test publik () {super (); } @Override Public String ToString () {return "test [id =" + id + ", pid =" + pid + ", text =" + text + "]"; }}4. Hasil Operasi
Data JSON:
{"checked": true, "children": [{"checked": false, "children": [{"checked": false, "children": [{"checked": false, "children": [{"checked": false, "children": {{'checked ": false," children ": []," ID "" 12, "12," 12 Parent ":" "Teks": "String"}, {"diperiksa": false, "anak -anak": [], "id": "18", "Parent": true, "ParentId": "3", "State": "Open", "Text": "dll"}, "Id": "3", "Parent": True, "ParentId": "1," "": ":": "3", "Parent": True, "ParentId": "1," "" ":": ":" Parent ": True," ParentId ":" 1, "" {state ":": "{state": "{state": "{state": "{{{" " false, "anak -anak": [{"diperiksa": false, "anak -anak": [], "id": "13", "Parent": true, "ParentId": "4", "State": "Open", "Text": "SQL"}, "": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": ":": "" "" "etc" } ], "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": "etc" } ], "id": "5", "Parent": True, "ParentId": "1", "State": "Open", "Text": "Spring"}, {"diperiksa": false, "anak -anak": [], "id": "6", "Parent": true, "ParentId": "1", "State": "Open", "FALT":: "FALUD": "PUTIGT": "1", "FALUD": "PUTICT": "FALUD": "1", "FALUD": "FALUD": "FALUD": " [], "id": "7", "Parent": true, "ParentId": "1", "state": "open", "text": "fastdfs"}, {"checked": false, "children": [], "id": "8", "Parent": true, "Parentid": "1", "" "" "", "Parent": "True," Parentid ":" 1 "," State "" "" "," Parent ":" Parentid ":" 1 "," state "" state "" "," fiSure ": linux," parentid ":" 1 "," state "" state "" "" "{" {"1" [], "id": "16", "Parent": True, "Parentid": "1", "State": "Open", "Text": "dll"}], "id": "1", "Parent": True, "Parentid": "", "State": "Open", "Teks": "Pembelajaran Teknis"}, {"", ":" FALCHED "," TEKS ":" Teknis "}," "," "ID": "9", "Parent": True, "ParentId": "2", "State": "Open", "Text": "Cycling"}, {"diperiksa": false, "anak -anak": [], "id": "10", "FALUT": "Parentid": "2", "State": "Open", "FALU,": "FALE," MINUM PETOR ":" 2 "," State ":" Open "," ":" ":": ":" FALE, "MOUDING": "2", "State": "Open", "": "FALE," FALE, "FALE," FUNING, "MINUK," 2 " "children": [], "id": "11", "parent": true, "parentId": "2", "state": "open", "text": "learning" }, { "checked": false, "children": [], "id": "17", "parent": true, "parentId": "2", "state": "open", "text": "etc" } ], "id": "2", "parent": Benar, "Parentid": "0", "State": "Open", "Text": "Interest"}], "Id": "0", "Parent": False, "Parentid": "", "State": "Open", "Text": "tentang saya"}], "id": "-1", "Parent": false, "Parentid" "," ",": ":": "-1", "Parent": false, "Parentid": "", "": ":": ":" -1, "": "": "" ":" ":": ":" —1, "Di atas adalah konten lengkap dari metode Java untuk menghasilkan data JSON struktur pohon berdasarkan konten tabel database. Saya harap semua orang dapat mendukung wulin.com ~