gpt4 book ai didi

java - 使用 Java 8 Stream 返回无副作用的 Map of Map

转载 作者:搜寻专家 更新时间:2023-11-01 01:39:41 26 4
gpt4 key购买 nike

如何将带有键 oldReportIdeServiceReportsMapByBatchFile 放入/添加到 eServiceReportMap 而没有副作用?

Map<String, Map<String, Set<EServiceReport>>> eServiceReportMap = new HashMap<>();
reports.forEach(report -> {
String oldReportId = report.getOldId();
Map<String, Set<EServiceReport>> eServiceReportsMapByBatchFile = // processing of batch files
...
eServiceReportMap.put(oldReportId, eServiceReportsMapByBatchFile);
});

return eServiceReportMap;

也就是我想让它变成这样:

return reports.stream()
.map(report -> {
String oldReportId = report.getOldId();
Map<String, Set<EServiceReport>> eServiceReportsMapByBatchFile = // processing of batch files
...
// I don't know how and what to return here
}).collect(// I don't know what to do here);

谢谢。

最佳答案

您最期待的是 Collectors.toMap,它可以用作:

return reports.stream()
.collect(Collectors.toMap(report -> report.getOldId(),
report -> {
// batch processing for eServiceReportsMapByBatchFile
return eServiceReportsMapByBatchFile;
}));

关于java - 使用 Java 8 Stream 返回无副作用的 Map of Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54565175/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com