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

标签 java java-8 hashmap java-stream

如何将带有键 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/

相关文章:

java - 由于 NullPointerException : Attempt to invoke virtual method HashMap on a null object reference 无法启动 Activity

java - 使用 java 流从 HashMap 获取特定键

java - 此 HQL 查询不适用于元素或成员,但 native 查询可以

java - 谓词与抽象类类型定义之间的 Hibernate 标准构建器

java - 在 Android studio 中使用 Activity 模板时无法添加新的 Java 类吗?

java - javafx中的可编辑 ListView

java - 使用 Java 8 Stream API 从对象列表中收集列表

Java:从 HashMap 读取可以改变其状态吗?

java - 以 int 值作为键的 HashMap

java - 无法在 Java : "pathLenConstraint violated - this cert must be the last cert in the certification path" 中对 SSL 站点进行身份验证