您可以使用flatMap操作符来展平integrityLevelSubtotal的结构,然后使用collect方法将其转换为Map<String, Map<String, StatisticsResultDTO>>

以下是一个示例代码:

import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        Map<String, Map<String, Map<String, StatisticsResultDTO>>> integrityLevelSubtotal = new HashMap<>();

        // 假设 integrityLevelSubtotal 已经有数据了
        // ...

        Map<String, Map<String, StatisticsResultDTO>> result = integrityLevelSubtotal.entrySet().stream()
                .flatMap(entry -> entry.getValue().entrySet().stream()
                        .map(innerEntry -> new AbstractMap.SimpleEntry<>(entry.getKey(), innerEntry)))
                .collect(Collectors.toMap(
                        entry -> entry.getKey(),
                        entry -> Collections.singletonMap(entry.getValue().getKey(), entry.getValue().getValue()),
                        (map1, map2) -> {
                            map1.putAll(map2);
                            return map1;
                        }
                ));

        // 输出结果
        result.forEach((key1, value) -> {
            System.out.println(key1);
            value.forEach((key2, dto) -> {
                System.out.println("  " + key2 + ": " + dto);
            });
        });
    }
}

class StatisticsResultDTO {
    // 定义 StatisticsResultDTO 的属性
}

在这个示例中,我们使用entrySet方法来获取integrityLevelSubtotal的键值对集合。然后使用flatMap操作符将每个键值对的中间Map展平为一个Stream。在展平时,我们使用AbstractMap.SimpleEntry来创建一个新的键值对,其中键是integrityLevelSubtotal的键,值是中间Map的键值对。最后,我们使用collect方法将展平后的结果转换为Map<String, Map<String, StatisticsResultDTO>>

请注意,我们还提供了一个合并函数,以处理可能存在的键冲突。在这个示例中,我们使用map1.putAll(map2)来将两个Map合并为一个。您可以根据实际需求自定义合并逻辑。


原文地址: http://www.cveoy.top/t/topic/lTnZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录