在项目我目前的工作,我们有循环这个构造三个嵌套的:3嵌套的for-each循环为Java流(或更好的并行流)
List<OutputDataType> resultList = new ArrayList<>();
for (OrgStructureEntity product : products) {
for (String region : regions) {
for (SalesType salesType : SalesType.values()) {
resultList.addAll(new SalesRequest(getConnection(),
product.getProductMainGroup(), product.getSbu(), planYear, currentPeriod, region, salesType,
exchangeRates).calculateSalesKpis());
}
}
}
产品和地区都设置。 resultList是一个带有“OutputDataType”对象的ArrayList。 方法calculateSalesKpis()也返回“OutputDataType”对象的列表。 所有这些对象都应该添加到resultList中。我想做到这一切与并行流,使其更快,但我没有得到多少比这再:
products.stream()
.map(product -> regions.stream()
.map(region -> Arrays.stream(SalesType.values())
.map(salesType -> new SalesRequest(getConnection(),
product.getProductMainGroup(), product.getSbu(), planYear, currentPeriod, region, salesType,
exchangeRates).calculateSalesKpis())))
.
我不知道如何把它所有在结果列表现在如何正确关闭流。 我希望你能帮助我:)
使用.collect(收藏家.toList())终端操作来收集列表中的结果 –
你是说最后一个点在哪里?这不幸的是不起作用... – Bofrostmann