Map<String, OrganizationLevelVo> data = organizationFeignService.getResourceLevelCity("1533835007229984").getData(); List<OrganizationVo> voList = organizationFeignService.getLargeScreenResourceCity().getData(); List<String> ids = voList.stream().map(OrganizationVo::getId).collect(Collectors.toList()); List<OrganizationLevelVo> vos = new ArrayList<>(); for (String s : data.keySet()) { for (String id : ids) { if (s.equals(id)){ vos.add(data.get(s)); } } }优化一下代码
可以使用 Java 8 的流式操作来简化代码,将两个循环合并为一个,并使用过滤器来筛选符合条件的元素,代码可优化为:
List<OrganizationLevelVo> vos = data.entrySet().stream()
.filter(entry -> ids.contains(entry.getKey()))
.map(Map.Entry::getValue)
.collect(Collectors.toList());
其中,entrySet()
方法返回映射中包含的映射关系的 Set 视图,filter()
方法根据条件过滤元素,map()
方法将元素映射为另一个元素,Collectors.toList()
方法将结果收集为列表。
优化这段代码:List<CompletableFuture<CallIntersectionVo>> futureList = Lists.newArrayList(); for (Map.Entry<String, List<String>> entry : intersectionResult.entrySet()) { CompletableFuture<CallIntersectionVo> future = CompletableFuture.supplyAsync(() -> { String account = entry.getKey(); List<String> personNoList = entry.getValue().stream().distinct().collect(Collectors.toList()); CallIntersectionVo vo = new CallIntersectionVo(); if (personNoList.size() >= 2) { List<PersonBasicVo> personVoList = Lists.newArrayList(); int count = 0; for (String personNo : personNoList) { Map<String, Object> callMap = callMapList.stream().filter(map -> personNo.equals(map.get("personNo"))).findAny().get(); List<CallRecord> callList = (List<CallRecord>) callMap.get("callList"); // 统计通话频率 count += callList.stream().filter(x -> account.equals(x.getRelationshipAccount())).count(); // 获取涉案人 personVoList.add(personList.stream().filter(person -> personNo.equals(person.getPersonNo())).findAny().get()); } // 共同号码是否属于涉案人 String commonPersonName = getCommonPersonName(personList, account); if (frequency != null && frequency > 0) { if (count >= frequency) { vo.setPersons(personVoList); vo.setCommonAccount(account); vo.setFrequency(count); vo.setCommonPersonName(commonPersonName); } return vo; } else { vo.setPersons(personVoList); vo.setCommonAccount(account); vo.setFrequency(count); vo.setCommonPersonName(commonPersonName); return vo; } } else { return vo; } }, executor); futureList.add(future); } voList.addAll(futureList.stream().map(CompletableFuture::join) .distinct().sorted(Comparator.comparing(vo -> vo.getPersons().size())) .collect(Collectors.toList()));
优化后的代码如下:
List<CompletableFuture<CallIntersectionVo>> futureList = intersectionResult.entrySet().stream()
.map(entry -> CompletableFuture.supplyAsync(() -> {
String account = entry.getKey();
List<String> personNoList = entry.getValue().stream().distinct().collect(Collectors.toList());
CallIntersectionVo vo = new CallIntersectionVo();
if (personNoList.size() >= 2) {
List<PersonBasicVo> personVoList = personNoList.stream()
.map(personNo -> {
Map<String, Object> callMap = callMapList.stream()
.filter(map -> personNo.equals(map.get("personNo")))
.findAny()
.orElseThrow(() -> new NoSuchElementException("Person not found"));
List<CallRecord> callList = (List<CallRecord>) callMap.get("callList");
int count = (int) callList.stream().filter(x -> account.equals(x.getRelationshipAccount())).count();
return personList.stream()
.filter(person -> personNo.equals(person.getPersonNo()))
.findAny()
.orElseThrow(() -> new NoSuchElementException("Person not found"));
})
.collect(Collectors.toList());
String commonPersonName = getCommonPersonName(personList, account);
vo.setPersons(personVoList);
vo.setCommonAccount(account);
vo.setFrequency(personVoList.size());
vo.setCommonPersonName(commonPersonName);
return vo;
} else {
return vo;
}
}, executor))
.collect(Collectors.toList());
List<CallIntersectionVo> voList = futureList.stream()
.map(CompletableFuture::join)
.distinct()
.sorted(Comparator.comparingInt(vo -> vo.getPersons().size()))
.collect(Collectors.toList());
主要优化:
- 使用流式编程,替换原来的for循环和entrySet遍历。
- 使用
stream()
和collect(Collectors.toList())
方法来收集结果,代替手动添加到列表中。 - 使用
orElseThrow
方法来处理可能找不到元素的情况,避免空指针异常。 - 将lambda表达式内联,使代码更简洁。
- 使用
Comparator.comparingInt
方法来指定排序规则,避免编译器警告。
/**循环递归**/ 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; }
这段代码是一个 Java 方法的示例,用于实现循环递归查询部门列表。代码中使用了一个 PrpdCompanyPo
类表示公司信息,使用了一个 PrpdCompanyVo
类表示返回的部门列表。
方法名为 deptList
,接受一个参数 comCode
,表示公司代码。该方法首先通过调用 selectByPid
方法查询指定公司代码的下级公司列表,并将其转换为 PrpdCompanyVo
对象的列表。
如果下级公司列表为空,说明当前公司是根节点,将其添加到结果列表中并返回。
如果下级公司列表不为空,将其转换为 PrpdCompanyVo
对象的列表,并遍历每个下级公司。对每个下级公司,再次调用 selectByPid
方法查询其下级公司列表,并将其转换为 PrpdCompanyVo
对象的列表。将这个列表设置为当前公司的子部门列表,并递归调用 buildChild
方法处理该子部门列表。
buildChild
方法也是一个递归方法,用于构建子部门列表。如果子部门列表为空,将当前部门添加到结果列表中并返回。如果子部门列表不为空,将其转换为 PrpdCompanyVo
对象的列表,并对每个子部门递归调用 buildChild
方法处理。
最后,通过调用 converVO
方法将 PrpdCompanyPo
对象转换为 PrpdCompanyVo
对象。
请注意,这只是一个示例代码,具体的实现可能需要根据您的数据结构和业务需求进行适当地修改。希望这对您有所帮助!如果您有其他问题,请随时提问。
相关推荐
















