本篇文章為大家展示了java中怎么遞歸部門樹,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)公司主營原州網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app軟件開發(fā),原州h5微信小程序開發(fā)搭建,原州網(wǎng)站營銷推廣歡迎原州等地區(qū)企業(yè)咨詢
package com.ytx.demo.tree; import java.util.ArrayList; import java.util.List; public class Department { private int id; private String name; private int parentId; private List<Department> children = new ArrayList<Department>(); public Department(int id, String name, int parentId) { this.id = id; this.name = name; this.parentId = parentId; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getParentId() { return parentId; } public void setParentId(int parentId) { this.parentId = parentId; } public List<Department> getChildren() { return children; } public void setChildren(List<Department> children) { this.children = children; } }
package com.ytx.demo.tree; import java.util.ArrayList; import java.util.List; public class DempartmentThree { public static void main(String[] args) { List<Department> departmentList = new ArrayList<>(); departmentList.add(new Department(1, "研發(fā)部門", 0)); departmentList.add(new Department(2, "研發(fā)團隊1", 1)); departmentList.add(new Department(3, "研發(fā)團隊2", 1)); departmentList.add(new Department(4, "財務(wù)部門", 0)); departmentList.add(new Department(5, "財務(wù)A部門", 4)); departmentList.add(new Department(6, "財務(wù)B部門", 4)); departmentList.add(new Department(7, "財務(wù)A部門團隊1", 5)); departmentList.add(new Department(8, "財務(wù)A部門團隊2", 5)); departmentList.add(new Department(9, "財務(wù)B部門團隊1", 6)); departmentList.add(new Department(10, "財務(wù)B部門團隊2", 6)); List<Department> listTree = getThree(departmentList,0); System.out.println(listTree); } private static List<Department> getThree(List<Department> list,int parentId){ //獲取所有子節(jié)點 List<Department> childTreeList = getChildTree(list,parentId); for (Department dept:childTreeList) { dept.setChildren(getThree(list,dept.getId())); } return childTreeList; } private static List<Department> getChildTree(List<Department> list,int id){ List<Department> childTree = new ArrayList<>(); for (Department dept:list) { if(dept.getParentId() == id){ childTree.add(dept); } } return childTree; } }
上述內(nèi)容就是java中怎么遞歸部門樹,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站名稱:java中怎么遞歸部門樹
本文來源:http://www.rwnh.cn/article34/jisipe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、品牌網(wǎng)站制作、企業(yè)網(wǎng)站制作、網(wǎng)站策劃、服務(wù)器托管、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)