要将外层的Map中的内层Map的key作为字典的key,可以使用Stream的flatMap和collect方法来实现。下面是一种可能的实现方式:

Map<String, StatisticsResultDTO> result = map.entrySet().stream()
        .flatMap(entry -> entry.getValue().entrySet().stream()
                .map(innerEntry -> new AbstractMap.SimpleEntry<>(innerEntry.getKey(), innerEntry.getValue())))
        .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));

在这个实现中,我们首先使用entrySet方法将外层Map转换为Stream,然后使用flatMap方法将内层Map转换为Stream。对于内层Map的每个键值对,我们将其转换为一个包含键和值的SimpleEntry对象。最后,使用collect方法将这些SimpleEntry对象收集为新的Map,其中键是内层Map的key,值是StatisticsResultDTO对象。

MapString MapString StatisticsResultDTO 使用stream转为MapString StatisticsResultDTO 里层的key作为字典的key

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

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