/**循环递归**/ public List<PrpdCompanyVo> deptList(String comCode) { /**查询指定公司代码的**/ List<PrpdCompanyPo> list0 = this.selectByPid(comCode); PrpdCompanyVo prpdCompanyVo = new PrpdCompanyVo(); prpdCompanyVo.setComCode(comCode); prpdCompanyVo.setIsEnable("Y"); if(CollectionUtils.isEmpty(list0)){ List<PrpdCompanyVo> listVo = new ArrayList<>(); listVo.add(prpdCompanyVo); return listVo; //根节点是空 //返回自己 } List<PrpdCompanyVo> voList=list0.stream().map(this::converVO).collect(Collectors.toList()); List<PrpdCompanyVo> voListBackup =list0.stream().map(this::converVO).collect(Collectors.toList()); voListBackup.forEach(item-> buildChild(item,voList)); return voList; } private void buildChild(PrpdCompanyVo item, List<PrpdCompanyVo> voListO){ List<PrpdCompanyPo> listChild = this.selectByPid(item.getComCode()); if(CollectionUtils.isEmpty(listChild)){ voListO.add(item); return; } List<PrpdCompanyVo> voList=listChild.stream().map(this::converVO).collect(Collectors.toList()); item.setChildList(voList); voList.forEach(item2-> buildChild(item2,voListO)); } private PrpdCompanyVo converVO(PrpdCompanyPo po){ PrpdCompanyVo VO = new PrpdCompanyVo(); BeanUtils.copyProperties(po,VO); return VO; }
时间: 2024-04-14 17:29:49 浏览: 15
这段代码是一个 Java 方法的示例,用于实现循环递归查询部门列表。代码中使用了一个 `PrpdCompanyPo` 类表示公司信息,使用了一个 `PrpdCompanyVo` 类表示返回的部门列表。
方法名为 `deptList`,接受一个参数 `comCode`,表示公司代码。该方法首先通过调用 `selectByPid` 方法查询指定公司代码的下级公司列表,并将其转换为 `PrpdCompanyVo` 对象的列表。
如果下级公司列表为空,说明当前公司是根节点,将其添加到结果列表中并返回。
如果下级公司列表不为空,将其转换为 `PrpdCompanyVo` 对象的列表,并遍历每个下级公司。对每个下级公司,再次调用 `selectByPid` 方法查询其下级公司列表,并将其转换为 `PrpdCompanyVo` 对象的列表。将这个列表设置为当前公司的子部门列表,并递归调用 `buildChild` 方法处理该子部门列表。
`buildChild` 方法也是一个递归方法,用于构建子部门列表。如果子部门列表为空,将当前部门添加到结果列表中并返回。如果子部门列表不为空,将其转换为 `PrpdCompanyVo` 对象的列表,并对每个子部门递归调用 `buildChild` 方法处理。
最后,通过调用 `converVO` 方法将 `PrpdCompanyPo` 对象转换为 `PrpdCompanyVo` 对象。
请注意,这只是一个示例代码,具体的实现可能需要根据您的数据结构和业务需求进行适当地修改。希望这对您有所帮助!如果您有其他问题,请随时提问。
相关问题
/**循环递归**/ public List<PrpdCompanyVo> deptList(String comCode) { /**查询指定公司代码的**/ List<PrpdCompanyPo> list0 = this.selectByPid(comCode); PrpdCompanyVo prpdCompanyVo = new PrpdCompanyVo(); prpdCompanyVo.setComCode(comCode); prpdCompanyVo.setIsEnable("Y"); if(CollectionUtils.isEmpty(list0)){ List<PrpdCompanyVo> listVo = new ArrayList<>(); listVo.add(prpdCompanyVo); return listVo; //根节点是空 //返回自己 } List<PrpdCompanyVo> voList=list0.stream().map(this::converVO).collect(Collectors.toList()); List<PrpdCompanyVo> voListBackup =list0.stream().map(this::converVO).collect(Collectors.toList()); voListBackup.forEach(item-> buildChild(item,voList)); return voList; } private void buildChild(PrpdCompanyVo item, List<PrpdCompanyVo> voListO){ List<PrpdCompanyPo> listChild = this.selectByPid(item.getComCode()); if(CollectionUtils.isEmpty(listChild)){ voListO.add(item); return; } List<PrpdCompanyVo> voList=listChild.stream().map(this::converVO).collect(Collectors.toList()); item.setChildList(voList); voList.forEach(item2-> buildChild(item2,voListO)); } private PrpdCompanyVo converVO(PrpdCompanyPo po){ PrpdCompanyVo VO = new PrpdCompanyVo(); BeanUtils.copyProperties(po,VO); return VO; } 请优化这段代码
在给出代码的优化之前,我注意到在 `buildChild` 方法中,您使用了 `selectByPid` 方法来查询子部门列表,但在 `deptList` 方法中也使用了相同的方法来查询下级公司列表。这可能会导致在构建子部门列表时重复查询相同的数据,导致性能下降。
为了优化代码,我建议您在 `deptList` 方法中先查询一次下级公司列表,并将其作为参数传递给 `buildChild` 方法。这样可以避免重复查询数据,提高代码效率。
另外,您在 `buildChild` 方法中使用了两个列表 `voList` 和 `voListO` 来存储子部门列表和结果列表。这样会导致内存占用较高。为了优化内存使用,您可以将 `voList` 和 `voListO` 合并为一个列表,并在每次递归调用 `buildChild` 方法之前,先将子部门列表添加到结果列表中。
以下是优化后的代码示例:
```java
public List<PrpdCompanyVo> deptList(String comCode) {
List<PrpdCompanyPo> list0 = this.selectByPid(comCode);
PrpdCompanyVo prpdCompanyVo = new PrpdCompanyVo();
prpdCompanyVo.setComCode(comCode);
prpdCompanyVo.setIsEnable("Y");
if (CollectionUtils.isEmpty(list0)) {
List<PrpdCompanyVo> listVo = new ArrayList<>();
listVo.add(prpdCompanyVo);
return listVo;
}
List<PrpdCompanyVo> voList = list0.stream().map(this::converVO).collect(Collectors.toList());
buildChild(voList, voList);
return voList;
}
private void buildChild(List<PrpdCompanyVo> itemList, List<PrpdCompanyVo> result) {
List<PrpdCompanyVo> childList = new ArrayList<>();
for (PrpdCompanyVo item : itemList) {
List<PrpdCompanyPo> listChild = this.selectByPid(item.getComCode());
if (CollectionUtils.isEmpty(listChild)) {
result.add(item);
} else {
List<PrpdCompanyVo> voList = listChild.stream().map(this::converVO).collect(Collectors.toList());
item.setChildList(voList);
childList.addAll(voList);
}
}
if (!childList.isEmpty()) {
buildChild(childList, result);
}
}
private PrpdCompanyVo converVO(PrpdCompanyPo po) {
PrpdCompanyVo vo = new PrpdCompanyVo();
BeanUtils.copyProperties(po, vo);
return vo;
}
```
在优化后的代码中,我们将 `voList` 和 `voListO` 合并为一个列表 `result`,并将子部门列表 `childList` 用于存储待处理的子部门。在每次递归调用 `buildChild` 方法之前,将子部门列表添加到结果列表 `result` 中。
这样就避免了重复查询数据,并且减少了内存占用。希望这个优化能对您有所帮助!如果您有其他问题,请随时提问。
string 转 List<Map<string,List<Map<string,List<Map<string,string>>>>>>
这个转换过程比较复杂,需要按照字符串的格式进行解析。以下是一个可能的实现方式:
```java
public static List<Map<String, List<Map<String, List<Map<String, String>>>>>> convert(String input) {
List<Map<String, List<Map<String, List<Map<String, String>>>>>> result = new ArrayList<>();
// 先按照大括号和逗号分割字符串
String[] parts = input.split("[{},]");
int level = 0;
Map<String, List<Map<String, List<Map<String, String>>>>> currentMap = null;
List<Map<String, List<Map<String, String>>>> currentList = null;
Map<String, List<Map<String, String>>> innerMap = null;
List<Map<String, String>> innerList = null;
for (String part : parts) {
part = part.trim();
if (part.isEmpty()) {
continue;
}
if (part.equals("[")) {
if (level == 0) {
currentMap = new LinkedHashMap<>();
result.add(currentMap);
} else if (level == 1) {
currentList = new ArrayList<>();
currentMap.put(currentList.size() + "", currentList);
} else if (level == 2) {
innerMap = new LinkedHashMap<>();
currentList.add(innerMap);
} else if (level == 3) {
innerList = new ArrayList<>();
innerMap.put(innerList.size() + "", innerList);
}
level++;
} else if (part.equals("]")) {
level--;
if (level == 1) {
currentList = null;
} else if (level == 2) {
innerMap = null;
} else if (level == 3) {
innerList = null;
}
} else {
// 解析每个元素的 key 和 value
String[] kv = part.split(":");
String key = kv[0].trim().replaceAll("\"", "");
String value = kv[1].trim().replaceAll("\"", "");
if (level == 1) {
currentMap.put(key, new ArrayList<>());
} else if (level == 2) {
innerMap.put(key, new ArrayList<>());
} else if (level == 3) {
innerList.add(new LinkedHashMap<>());
} else if (level == 4) {
Map<String, String> innerInnerMap = new LinkedHashMap<>();
innerList.add(innerInnerMap);
String[] innerKv = value.split(",");
for (String innerPart : innerKv) {
String[] innerKv2 = innerPart.split(":");
String innerKey = innerKv2[0].trim().replaceAll("\"", "");
String innerValue = innerKv2[1].trim().replaceAll("\"", "");
innerInnerMap.put(innerKey, innerValue);
}
}
}
}
return result;
}
```
这个方法先按照大括号和逗号分割字符串,然后使用一个 `level` 变量记录当前解析到哪一层。对于每个元素,根据 `level` 的值来判断它是一个列表还是一个映射,然后递归解析。最后返回一个 `List<Map<String, List<Map<String, List<Map<String, String>>>>>>` 类型的结果。