本篇文章為大家展示了java中怎么遞歸部門樹,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(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 Listchildren = new ArrayList (); 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 getChildren() { return children; } public void setChildren(List 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) { ListdepartmentList = new ArrayList<>(); departmentList.add(new Department(1, "研發(fā)部門", 0)); departmentList.add(new Department(2, "研發(fā)團(tuán)隊(duì)1", 1)); departmentList.add(new Department(3, "研發(fā)團(tuán)隊(duì)2", 1)); departmentList.add(new Department(4, "財(cái)務(wù)部門", 0)); departmentList.add(new Department(5, "財(cái)務(wù)A部門", 4)); departmentList.add(new Department(6, "財(cái)務(wù)B部門", 4)); departmentList.add(new Department(7, "財(cái)務(wù)A部門團(tuán)隊(duì)1", 5)); departmentList.add(new Department(8, "財(cái)務(wù)A部門團(tuán)隊(duì)2", 5)); departmentList.add(new Department(9, "財(cái)務(wù)B部門團(tuán)隊(duì)1", 6)); departmentList.add(new Department(10, "財(cái)務(wù)B部門團(tuán)隊(duì)2", 6)); List listTree = getThree(departmentList,0); System.out.println(listTree); } private static List getThree(List list,int parentId){ //獲取所有子節(jié)點(diǎn) List childTreeList = getChildTree(list,parentId); for (Department dept:childTreeList) { dept.setChildren(getThree(list,dept.getId())); } return childTreeList; } private static List getChildTree(List list,int id){ List childTree = new ArrayList<>(); for (Department dept:list) { if(dept.getParentId() == id){ childTree.add(dept); } } return childTree; } }
上述內(nèi)容就是java中怎么遞歸部門樹,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。