简体   繁体   English

将CollectingAndThen()与Collectors.toMap()结合使用需要显式类型转换

[英]Using collectingAndThen() with Collectors.toMap() requires explicit type cast

I have a Map<String,BigDecimal> (say amountMap) which I want to convert to an ImmutableMap<String,Double> , code for which is: 我有一个Map<String,BigDecimal> (例如amountMap),我想将其转换为ImmutableMap<String,Double> ,其代码为:

return amountMap.entrySet().stream()
        .collect(collectingAndThen(toMap(e->e.getKey(),e->e.getValue().doubleValue()),ImmutableMap::copyOf));

However Eclipse shows an error which says that e.getKey() and e.getValue() require explicit type casting since they are of type Object. 但是Eclipse显示了一个错误,指出e.getKey()和e.getValue()需要显式类型转换,因为它们属于Object类型。

The same code works when I split it like so: 当我像这样分割它时,相同的代码也可以工作:

Map<String,Double> tempMap = amountMap.entrySet().stream()
                                .collect(toMap(e->e.getKey(),e->e.getValue().doubleValue());

return ImmutableMap.copyOf(tempMap);

I am assuming the former error is because of Type Erasure, but if not, is there a way to return the Map as an ImmutableMap without the intermediate step of creating a temporary map to hold the results ? 我假设前一个错误是由于Type Erasure引起的,但是如果不是这样,是否有一种方法可以将Map作为ImmutableMap返回,而无需执行创建临时映射以保存结果的中间步骤?

这是因为我使用的是旧版本的Eclipse(Luna)已在升级时修复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM