Jul*_*lez 4 java hashmap java-8 java-stream
如何在没有副作用的情况下添加/添加eServiceReportsMapByBatchFile键?oldReportIdeServiceReportMap
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;
Run Code Online (Sandbox Code Playgroud)
也就是说,我希望它变成这样:
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);
Run Code Online (Sandbox Code Playgroud)
谢谢.
你期待的主要是Collectors.toMap可以用作:
return reports.stream()
.collect(Collectors.toMap(report -> report.getOldId(),
report -> {
// batch processing for eServiceReportsMapByBatchFile
return eServiceReportsMapByBatchFile;
}));
Run Code Online (Sandbox Code Playgroud)