优化以下代码: //获取所有的类目ids List<Long> goodsCategoryIds = new ArrayList<>(); //获取所有的商品ids List<Long> goodsId = new ArrayList<>(); if (!CollectionUtils.isEmpty(buyReturnActivity.getGoodsIds())) { goodsId.addAll(buyReturnActivity.getGoodsIds()); } if (!CollectionUtils.isEmpty(buyReturnActivity.getGoodsCategoryIds())) { goodsCategoryIds.addAll(buyReturnActivity.getGoodsCategoryIds()); } //获取类目的所有ids Set<Long> goodsCategoryId = new HashSet<>(); if (!CollectionUtils.isEmpty(goodsCategoryIds)) { log.info("获取到的多买多返的类目Ids为============" + goodsCategoryIds); Map<Long, CategoryCache> categoryMap2 = this.categoryService.categoryMap(tenantId, goodsCategoryIds); log.info("获取到的类目缓存mapwei ======= " + categoryMap2); goodsCategoryIds.forEach(c -> { if (Objects.isNull(categoryMap2.get(c))) { return; } else { CategoryCache categoryCache1 = categoryMap2.get(c); String[] split = categoryCache1.getIdPaths().split("-"); List<String> strings = Arrays.asList(split); Set<Long> categoryId = strings.stream().map(Long::parseLong).collect(Collectors.toSet()); goodsCategoryId.addAll(categoryId); } }); }
时间: 2024-01-18 19:05:16 浏览: 22
可以优化的地方如下:
1. 可以使用addAll方法替换forEach循环中的if-else条件判断,减少代码量。
2. 可以使用流式编程简化代码逻辑,提高可读性。
重构后的代码如下:
```
List<Long> goodsCategoryIds = new ArrayList<>();
List<Long> goodsId = new ArrayList<>();
goodsId.addAll(buyReturnActivity.getGoodsIds());
goodsCategoryIds.addAll(buyReturnActivity.getGoodsCategoryIds());
Set<Long> goodsCategoryId = goodsCategoryIds.stream()
.map(c -> this.categoryService.categoryMap(tenantId, Collections.singletonList(c)).get(c))
.filter(Objects::nonNull)
.map(CategoryCache::getIdPaths)
.flatMap(s -> Arrays.stream(s.split("-")))
.map(Long::parseLong)
.collect(Collectors.toSet());
```
相关问题
List<CustomerInfo> customerInfoList = new ArrayList<>(); List<PostLoanOrder> orderList = new ArrayList<>(); List<PostControllerLegalInfo> plistInfo = new ArrayList<>(); List<PostLoanReceipt> receipts = new ArrayList<>(); List<PostRepaymentPlan> plans = new ArrayList<>(); List<AuthBatchAndSendDto> authBatchAndSendDtoList = new ArrayList<>(); 改进代码
可以使用 diamond operator 来简化代码,还可以使用 var 关键字来更清晰地表达变量类型:
```java
var customerInfoList = new ArrayList<CustomerInfo>();
var orderList = new ArrayList<PostLoanOrder>();
var plistInfo = new ArrayList<PostControllerLegalInfo>();
var receipts = new ArrayList<PostLoanReceipt>();
var plans = new ArrayList<PostRepaymentPlan>();
var authBatchAndSendDtoList = new ArrayList<AuthBatchAndSendDto>();
```
此外,如果这些变量都是在同一个类中使用的,可以将它们作为类的成员变量,这样代码会更加简洁。
// 分成 10 个线程并行处理数据 int threadNum = 10; int dataSizePerThread = awardIds.size() / threadNum; List<CompletableFuture<List<TCaAwardInfo>>> futures = new ArrayList<>(); for (int i = 0; i < threadNum; i++) { int startIndex = i * dataSizePerThread; int endIndex = (i == threadNum - 1) ? awardIds.size() : (i + 1) * dataSizePerThread; List<Long> subList = awardIds.subList(startIndex, endIndex); CompletableFuture<List<TCaAwardInfo>> future = CompletableFuture.supplyAsync(() -> { List<TCaAwardInfo> result = new ArrayList<>(); List<TCaAwardInfo> tCaAwardInfos = awardApiService.queryActivationCode(subList); result.addAll(tCaAwardInfos); return result; }, taskExecutor); futures.add(future); } // 等待所有线程执行完毕 CompletableFuture<Void> allFutures = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])); allFutures.join(); // 合并结果 List<TCaAwardInfo> activationCodeList = futures.stream() .flatMap(future -> future.join().stream()) .collect(Collectors.toList()); 优化一下这个代码
这段代码的并行处理数据的逻辑已经很好了,可以考虑在结果合并的时候进行优化。可以使用`CompletableFuture.join()`方法替代`CompletableFuture.join().stream()`,这样可以避免创建Stream对象,提高代码执行效率。同时,可以考虑使用`addAll()`方法替代`flatMap()`和`collect(Collectors.toList())`方法,从而避免创建Stream对象和List对象,提高代码执行效率。代码改进后如下:
```
int threadNum = 10;
int dataSizePerThread = awardIds.size() / threadNum;
List<CompletableFuture<List<TCaAwardInfo>>> futures = new ArrayList<>();
for (int i = 0; i < threadNum; i++) {
int startIndex = i * dataSizePerThread;
int endIndex = (i == threadNum - 1) ? awardIds.size() : (i + 1) * dataSizePerThread;
List<Long> subList = awardIds.subList(startIndex, endIndex);
CompletableFuture<List<TCaAwardInfo>> future = CompletableFuture.supplyAsync(() -> {
List<TCaAwardInfo> result = new ArrayList<>();
List<TCaAwardInfo> tCaAwardInfos = awardApiService.queryActivationCode(subList);
result.addAll(tCaAwardInfos);
return result;
}, taskExecutor);
futures.add(future);
}
List<TCaAwardInfo> activationCodeList = new ArrayList<>();
for (CompletableFuture<List<TCaAwardInfo>> future : futures) {
activationCodeList.addAll(future.join());
}
```