public ResponseEntity<List<Information>> list() throws Exception { List<Information> list = informationService.list(); List<Information> information=new ArrayList<>(); //如果不为空就递归赋值 if (CollectionUtil.isNotEmpty(list)) { information = assemblyTree(list); } return ResponseEntity.ok(information); } //先区分父子节点 public static List<Information> assemblyTree(List<Information> vos){ List<Information> dd = new ArrayList<>(); List<Information> zy = new ArrayList<>(); for (Information vo : vos) { if ("0".equals(vo.getParentId())){ dd.add(vo); }else { zy.add(vo); } } dd = assemblyTree(dd,zy); return dd; } // 对父节点进行追加子节点 public static List<Information> assemblyTree(List<Information> dd,List<Information> zy){ Map<String, List<Information>> zyMap = zy.stream().collect(Collectors.groupingBy(a -> a.getParentId())); for (Information d : dd) { List<Information> equipmentTemplateTreeVos = zyMap.get(d.getId()); if (null != equipmentTemplateTreeVos && equipmentTemplateTreeVos.size() != 0){ List<Information> cj = zy; cj.removeAll(equipmentTemplateTreeVos); equipmentTemplateTreeVos = assemblyTree(equipmentTemplateTreeVos,cj); d.setChildren(equipmentTemplateTreeVos); } } return dd; }
递归生成树
最新推荐文章于 2024-07-27 13:31:57 发布