首页 > 代码库 > 递归之遍历部门
递归之遍历部门
递归是一个非常有用的知识点。写点实例帮助自己记忆
中间有过程代码
首先一个javapojo类
1 package com.qcf.po; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class Depart { 7 8 private Long id; 9 private String name;10 private String destion;11 //用户12 Set<User> users=new HashSet<User>();13 14 //子类部门15 Set<Depart> departs=new HashSet<Depart>();16 17 //父类部门18 private Depart depart;19 20 public Long getId() {21 return id;22 }23 24 public void setId(Long id) {25 this.id = id;26 }27 28 public String getName() {29 return name;30 }31 32 public void setName(String name) {33 this.name = name;34 }35 36 public Set<User> getUsers() {37 return users;38 }39 40 public void setUsers(Set<User> users) {41 this.users = users;42 }43 44 public Set<Depart> getDeparts() {45 return departs;46 }47 48 public void setDeparts(Set<Depart> departs) {49 this.departs = departs;50 }51 52 public Depart getDepart() {53 return depart;54 }55 56 public void setDepart(Depart depart) {57 this.depart = depart;58 }59 60 public String getDestion() {61 return destion;62 }63 64 public void setDestion(String destion) {65 this.destion = destion;66 }67 68 69 70 }
测试代码:
1 package com.qcf.util; 2 3 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.HashSet; 6 import java.util.List; 7 import java.util.Set; 8 9 import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut; 10 11 import com.qcf.po.Depart; 12 13 public class PreDepart { 14 15 /** 16 * 结构 17 * ┝开发部 18 * ┝开发一部 19 * ┝开发一组 20 * ┝开发二组 21 * ┝开发二部 22 * ┝开发三组 23 * ┝开发四组 24 * ┝销售部 25 * ┝销售一部 26 * ┝销售二部 27 */ 28 public static List<Depart> findDepartTopList(){ 29 //顶级 30 Depart depart1=new Depart(); 31 depart1.setName("开发部"); 32 33 //二级 34 Depart depart1_1=new Depart(); 35 depart1_1.setName("开发一部"); 36 37 //三级 38 Depart depart1_1_1=new Depart(); 39 depart1_1_1.setName("开发一组"); 40 Depart depart1_1_2=new Depart(); 41 depart1_1_2.setName("开发二组"); 42 43 Set<Depart> departs1_1=new HashSet<Depart>(); 44 departs1_1.add(depart1_1_1); 45 departs1_1.add(depart1_1_2); 46 depart1_1.setDeparts(departs1_1); 47 48 Depart depart1_2=new Depart(); 49 depart1_2.setName("开发二部"); 50 51 Depart depart1_2_1=new Depart(); 52 depart1_2_1.setName("开发一组"); 53 Depart depart1_2_2=new Depart(); 54 depart1_2_2.setName("开发二组"); 55 56 Set<Depart> departs1_2=new HashSet<Depart>(); 57 departs1_2.add(depart1_1_1); 58 departs1_2.add(depart1_1_2); 59 depart1_2.setDeparts(departs1_1); 60 61 Set<Depart> departs1=new HashSet<Depart>(); 62 departs1.add(depart1_1); 63 departs1.add(depart1_2); 64 65 depart1.setDeparts(departs1); 66 67 //顶级 68 Depart depart2=new Depart(); 69 depart2.setName("销售部"); 70 //二级 71 Depart depart2_1=new Depart(); 72 depart2_1.setName("销售一部"); 73 Depart depart2_2=new Depart(); 74 depart2_2.setName("销售二部"); 75 76 Set<Depart> departs=new HashSet<Depart>(); 77 departs.add(depart2_1); 78 departs.add(depart2_2); 79 80 depart2.setDeparts(departs); 81 82 List<Depart> list=new ArrayList<Depart>(); 83 list.add(depart1); 84 list.add(depart2); 85 return list; 86 } 87 public static void main(String[] args) { 88 List<Depart> list=findDepartTopList(); 89 // for (Depart depart : list) { 90 // getAllDepartName(depart); 91 // } 92 getAllDepartName_2(list,"="); 93 } 94 95 /**将所有的部门打印出来*/ 96 public static void getAllDepartName(Depart depart){ 97 System.out.println(depart.getName()); 98 for (Depart chirden : depart.getDeparts()) { 99 getAllDepartName(chirden);100 }101 }102 103 /**将所有的部门打印出来*/104 public static void getAllDepartName_2(Collection<Depart> list,String pre){105 for (Depart depart2 : list) {106 System.out.println( pre +depart2.getName());107 getAllDepartName_2(depart2.getDeparts()," "+pre);108 }109 }110 }
递归之遍历部门
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。