简体   繁体   中英

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:

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.

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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