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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | package com.tledu.practice.day_19.test; import java.util.*; import java.util.stream.Collectors; /** * @author Mr.Wang * @date 23-3-25 */ public class Test_01 { /** * 给出示例数据,包括不同的学院,每个学院有不同的专业,每个专业有不同的班, * 现在要求用所学的集合的知识,把他们组装好,班级放到专业里,专业放到学院里 * @param args */ public static void main(String[] args) { //示例数据 List<SysDept> list=new ArrayList<>(); SysDept sysDept=new SysDept(0L,null,"信息学院"); SysDept sysDept22=new SysDept(1L,null,"影视学院"); SysDept sysDept1=new SysDept(101L,0L,"软件工程"); SysDept sysDept2=new SysDept(102L,0L,"计算机科学与技术"); SysDept sysDept3=new SysDept(103L,0L,"网络工程"); SysDept sysDept4=new SysDept(501L,101L,"软件1班"); SysDept sysDept5=new SysDept(502L,101L,"软件2班"); SysDept sysDept6=new SysDept(503L,101L,"软件3班"); SysDept sysDept7=new SysDept(601L,102L,"计算机1班"); SysDept sysDept8=new SysDept(602L,102L,"计算机2班"); SysDept sysDept9=new SysDept(603L,102L,"计算机3班"); SysDept sysDept10=new SysDept(701L,103L,"网络1班"); SysDept sysDept11=new SysDept(702L,103L,"网络2班"); SysDept sysDept12=new SysDept(703L,103L,"网络3班"); list.add(sysDept); list.add(sysDept1); list.add(sysDept2); list.add(sysDept3); list.add(sysDept4); list.add(sysDept5); list.add(sysDept6); list.add(sysDept7); list.add(sysDept8); list.add(sysDept9); list.add(sysDept10); list.add(sysDept11); list.add(sysDept12); list.add(sysDept22); // System.out.println(list); List<SysDept> deptTree = getDeptTree(list); System.out.println(deptTree); } /** * 演示代码 * @param list * @return */ public static List<SysDept> getDeptTree(List<SysDept> list) { Map<Long, SysDept> map = new HashMap<>(); // 获取id数组,和映射关系 //这里的stream流可替换成for循环 List<Long> idList = list.stream().map(sysDept -> { map.put(sysDept.getDeptId(), sysDept); return sysDept.getDeptId(); }).collect(Collectors.toList()); // map中装填了13项,k=部门id,v是自己部门对象 // idList中装填了13个id // 最终的数组 List<SysDept> deptList = new ArrayList<>(); list.forEach(sysDept -> { // 在这个集合中能找到它的父级 if (idList.contains(sysDept.getParentId())) { // 获取它的父级 SysDept dept = map.get(sysDept.getParentId()); // 如果没有子节点创建一个 if (dept.getChildren() == null) { dept.setChildren(new ArrayList<>()); } dept.getChildren().add(sysDept); }else{ // 如果找不到他的父级,那么说明它应该是根节点 deptList.add(sysDept); } }); // 如果没有放到父级数组中的数据,代表要放在第一级别 return deptList; } } |
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | package com.tledu.practice.day_19.test; import java.util.ArrayList; import java.util.List; /** * @author Mr.Wang * @date 23-3-25 */ public class SysDept implements Comparable<SysDept>{ /** 部门ID */ private Long deptId; /** 父部门ID */ private Long parentId; /** 祖级列表 */ private String ancestors; /** 部门名称 */ private String deptName; /** 负责人 */ private String leader; /** 新加属性 子部门 */ private List<SysDept> children ; public SysDept() { } @Override public String toString() { return "SysDept{" + "deptId=" + deptId + ", parentId=" + parentId + ", deptName='" + deptName + '\'' + '}'; } public SysDept(Long deptId, Long parentId, String deptName) { this.deptId = deptId; this.parentId = parentId; this.deptName = deptName; } public Long getDeptId() { return deptId; } public void setDeptId(Long deptId) { this.deptId = deptId; } public Long getParentId() { return parentId; } public void setParentId(Long parentId) { this.parentId = parentId; } public String getAncestors() { return ancestors; } public void setAncestors(String ancestors) { this.ancestors = ancestors; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public String getLeader() { return leader; } public void setLeader(String leader) { this.leader = leader; } public List<SysDept> getChildren() { return children; } public void setChildren(List<SysDept> children) { this.children = children; } @Override public int compareTo(SysDept o) { return 0; } } |
集合多级评论
最新推荐文章于 2025-06-08 23:08:17 发布