我想从地图映射转换内部地图。使用Java Stream API转换类型X到Y <K, Map<V, X>>使用Java Stream API
旧地图:Map<String, Map<LocalDate, Integer>>
整数表示秒
新地图:Map<String, Map<LocalDate, Duration>>
我曾尝试创建新的内部地图,却得到了一个错误
Error: java: no suitable method found for
putAll(java.util.stream.Stream<java.lang.Object>)
methodjava.util.Map.putAll(java.util.Map<? extends java.time.LocalDate,? extends java.time.Duration>)
is not applicable
oldMap.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey,
e -> new HashMap<LocalDate, Duration>() {{
putAll(
e.getValue().entrySet().stream()
.map(x -> new HashMap.SimpleEntry<LocalDate, Duration>
(x.getKey(), Duration.ofSeconds(x.getValue())))
);
}}
));
相关的问题:https://stackoverflow.com/questions/25864559/iterate-though-a-map-of-maps-with-lambda -in-java-8 –
相关(来自同一个OP):[使用Collectors.summingInt时如何获得自定义类型而不是Integer?](https: //stackoverflow.com/qu estions/44285623/how-to-get-a-custom-type-instead-of-integer-when-using-collectors-summingint) –