1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| 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, "研发部门", 0)); departmentList.add(new Department(2, "研发团队1", 1)); departmentList.add(new Department(3, "研发团队2", 1)); departmentList.add(new Department(4, "财务部门", 0)); departmentList.add(new Department(5, "财务A部门", 4)); departmentList.add(new Department(6, "财务B部门", 4)); departmentList.add(new Department(7, "财务A部门团队1", 5)); departmentList.add(new Department(8, "财务A部门团队2", 5)); departmentList.add(new Department(9, "财务B部门团队1", 6)); departmentList.add(new Department(10, "财务B部门团队2", 6));
List<Department> listTree = getThree(departmentList,0); System.out.println(listTree);
System.out.println("--------------"); }
private static List<Department> getThree(List<Department> list,int parentId){ 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; } }
|