如何将带有键 oldReportId
的 eServiceReportsMapByBatchFile
放入/添加到 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/