public ListDownStreamReportCollectVO searchDownApiSumBIReportSearchVo biReportSearchVo ListDownStreamReportCollectVO voList = biReportMappersearchApiReportListbiReportSearchVosetProductIdListL
这段代码可以进行优化。以下是一些可能的优化方法:
-
使用方法引用:可以使用方法引用来简化代码。例如,将
Function<Function<DownstreamAPIReportVO, Long>, Long>简化为ToLongFunction<DownstreamAPIReportVO>,将Function<Function<DownstreamAPIReportVO, BigDecimal>, BigDecimal>简化为Function<DownstreamAPIReportVO, BigDecimal>。 -
使用静态导入:通过使用静态导入,可以将代码更清晰地表达出来,减少代码量。
-
减少重复代码:可以将重复的代码提取到单独的方法中,以避免重复编写相同的代码。
-
使用并行流:如果数据量很大,可以考虑使用并行流来加快处理速度。
下面是经过优化的代码示例:
public List<DownStreamReportCollectVO> searchDownApiSum(BIReportSearchVo biReportSearchVo) {
List<DownStreamReportCollectVO> voList = biReportMapper.searchApiReportList(biReportSearchVo.setProductIdList(Lists.emptyList()))
.stream()
.collect(Collectors.groupingBy(DownstreamAPIReportVO::getProductName))
.entrySet()
.stream()
.map(item -> {
Supplier<Stream<DownstreamAPIReportVO>> sup = item.getValue()::stream;
// Long 求和
ToLongFunction<DownstreamAPIReportVO> longFun = t -> sup.get().mapToLong(tlf).filter(Objects::nonNull).sum();
// BigDecimal 求和
Function<DownstreamAPIReportVO, BigDecimal> bigFun = fn -> sup.get().map(fn).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
Long threeItemCheckCount = longFun.applyAsLong(DownstreamAPIReportVO::getThreeItemCheckCount);
Long authSuccessCount = longFun.applyAsLong(DownstreamAPIReportVO::getAuthSuccessCount);
BigDecimal authSuccessAmount = bigFun.apply(DownstreamAPIReportVO::getAuthSuccessAmount);
BigDecimal importIncome = bigFun.apply(DownstreamAPIReportVO::getImportTrafficIncome);
return new DownStreamReportCollectVO()
.setProductName(item.getKey())
.setApplyCount(threeItemCheckCount)
.setAuthingCount(longFun.applyAsLong(DownstreamAPIReportVO::getAuthingCount))
.setAuthSuccessCount(longFun.applyAsLong(DownstreamAPIReportVO::getThreeItemCheckPassCount))
.setAuthSuccessCount(authSuccessCount)
.setAuthSuccessAmount(authSuccessAmount)
.setAuthSuccessAmountAvg(authSuccessCount > 0 ? authSuccessAmount.divide(new BigDecimal(authSuccessCount), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO)
.setClickGetCashAccountCount(longFun.applyAsLong(DownstreamAPIReportVO::getClickGetCashAccountCount))
.setFirstLoanAccountCount(longFun.applyAsLong(DownstreamAPIReportVO::getFirstLoanAccountCount))
.setFirstLoanAmount(bigFun.apply(DownstreamAPIReportVO::getFirstLoanAmount))
.setAuthSuccessRate(BigDecimal.valueOf(Arith.div(authSuccessCount, threeItemCheckCount, 4)).multiply(new BigDecimal(100)))
.setImportTrafficIncome(importIncome)
.setSingleIncome(threeItemCheckCount > 0 ? importIncome.divide(new BigDecimal(threeItemCheckCount), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO);
}).collect(Collectors.toList());
Supplier<Stream<DownStreamReportCollectVO>> sup = voList::stream;
// Long 求和
ToLongFunction<DownStreamReportCollectVO> longFun = t -> sup.get().mapToLong(tlf).filter(Objects::nonNull).sum();
// BigDecimal 求和
Function<DownStreamReportCollectVO, BigDecimal> bigFun = fn -> sup.get().map(fn).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
Long applyCount = longFun.applyAsLong(DownStreamReportCollectVO::getApplyCount);
Long authSuccessCount = longFun.applyAsLong(DownStreamReportCollectVO::getAuthSuccessCount);
BigDecimal authSuccessAmount = bigFun.apply(DownStreamReportCollectVO::getAuthSuccessAmount);
BigDecimal importIncome = bigFun.apply(DownStreamReportCollectVO::getImportTrafficIncome);
return Stream.concat(Stream.of(new DownStreamReportCollectVO()
.setApplyCount(applyCount)
.setAuthingCount(longFun.applyAsLong(DownStreamReportCollectVO::getAuthingCount))
.setAuthSuccessCount(authSuccessCount)
.setAuthSuccessAmount(authSuccessAmount)
.setAuthSuccessAmountAvg(authSuccessCount > 0 ? authSuccessAmount.divide(new BigDecimal(authSuccessCount), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO)
.setClickGetCashAccountCount(longFun.applyAsLong(DownStreamReportCollectVO::getClickGetCashAccountCount))
.setFirstLoanAccountCount(longFun.applyAsLong(DownStreamReportCollectVO::getFirstLoanAccountCount))
.setFirstLoanAmount(bigFun.apply(DownStreamReportCollectVO::getFirstLoanAmount))
.setAuthSuccessRate(BigDecimal.valueOf(Arith.div(authSuccessCount, applyCount, 4)).multiply(new BigDecimal(100)))
.setImportTrafficIncome(importIncome)
.setSingleIncome(applyCount > 0 ? importIncome.divide(new BigDecimal(applyCount), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO)
), voList.stream()).collect(Collectors.toList());
}
请注意,由于我无法了解您的完整代码和相关环境,因此无法保证上述优化方法一定能提高性能。建议在进行任何优化之前,先进行性能测试和评估,以确保优化的有效性
原文地址: https://www.cveoy.top/t/topic/hNRB 著作权归作者所有。请勿转载和采集!