Java Stream API:将Map<String, Map<String, Map<String, StatisticsResultDTO>>>转换为Map<String, Map<String, StatisticsResultDTO>>并去除中间Map的key
您可以使用Java 8的Stream API来转换integrityLevelSubtotal,并去除中间Map的键。以下是一个示例代码:
Map<String, Map<String, StatisticsResultDTO>> result = integrityLevelSubtotal.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> entry.getValue().values().stream()
.findFirst().orElse(new StatisticsResultDTO())
));
这里使用entrySet().stream()将integrityLevelSubtotal转换为Stream<Map.Entry<String, Map<String, Map<String, StatisticsResultDTO>>>>。然后使用Collectors.toMap()将每个条目转换为Map<String, Map<String, StatisticsResultDTO>>。在toMap()中,我们使用entry.getKey()作为新Map的键。对于值,我们使用entry.getValue().values().stream()获取中间Map的值的Stream<Map<String, StatisticsResultDTO>>,然后使用findFirst().orElse(new StatisticsResultDTO())获取第一个StatisticsResultDTO对象。如果中间Map为空,我们将返回一个默认的StatisticsResultDTO对象。
请注意,这个示例假设中间的Map中只有一个条目。如果中间的Map中有多个条目,您可能需要根据自己的需求进行修改。
原文地址: http://www.cveoy.top/t/topic/lToe 著作权归作者所有。请勿转载和采集!