将 collectingAndThen() 与 Collectors.toMap() 一起使用需要显式类型转换
Using collectingAndThen() with Collectors.toMap() requires explicit type cast
我有一个 Map<String,BigDecimal>
(比如 amountMap),我想将其转换为 ImmutableMap<String,Double>
,代码为:
return amountMap.entrySet().stream()
.collect(collectingAndThen(toMap(e->e.getKey(),e->e.getValue().doubleValue()),ImmutableMap::copyOf));
但是 Eclipse 显示一个错误,指出 e.getKey() 和 e.getValue() 需要显式类型转换,因为它们是 Object 类型。
当我这样拆分时,同样的代码有效:
Map<String,Double> tempMap = amountMap.entrySet().stream()
.collect(toMap(e->e.getKey(),e->e.getValue().doubleValue());
return ImmutableMap.copyOf(tempMap);
我假设前一个错误是因为类型擦除,但如果不是,有没有办法 return 将地图作为 ImmutableMap 而无需创建临时地图来保存结果的中间步骤?
这是因为我使用的是旧版本的 Eclipse(Luna) 已修复升级
我有一个 Map<String,BigDecimal>
(比如 amountMap),我想将其转换为 ImmutableMap<String,Double>
,代码为:
return amountMap.entrySet().stream()
.collect(collectingAndThen(toMap(e->e.getKey(),e->e.getValue().doubleValue()),ImmutableMap::copyOf));
但是 Eclipse 显示一个错误,指出 e.getKey() 和 e.getValue() 需要显式类型转换,因为它们是 Object 类型。
当我这样拆分时,同样的代码有效:
Map<String,Double> tempMap = amountMap.entrySet().stream()
.collect(toMap(e->e.getKey(),e->e.getValue().doubleValue());
return ImmutableMap.copyOf(tempMap);
我假设前一个错误是因为类型擦除,但如果不是,有没有办法 return 将地图作为 ImmutableMap 而无需创建临时地图来保存结果的中间步骤?
这是因为我使用的是旧版本的 Eclipse(Luna) 已修复升级