【发布时间】:2017-05-03 23:24:32
【问题描述】:
除了我之前提出的问题,可以在这里找到, How to combine list elements and find the price of largest combination
我没有使用Integer price,而是使用String price,
List<Long> highest = details
.stream()
.map(d -> Stream.concat(Stream.of(d.getDetailId()), d.getStackableDetails().stream()).collect(Collectors.toList()))
.collect(Collectors.toMap(s -> s.stream().map(Double.class::cast).reduce(0D,
(left, right) -> left + Double.parseDouble(map.get(right).getPrice())),
s -> s.stream().collect(Collectors.toList()),
(left, right) -> right,
TreeMap::new))
.lastEntry().getValue();
但我在运行时不断收到类转换异常。有人可以告诉我为什么我无法转换 Stream 类型以及如何纠正它。谢谢!
【问题讨论】:
-
发布实际失败消息/堆栈跟踪
-
java.lang.ClassCastException: 无法将 java.lang.Long 转换为 java.lang.Double
-
是的,例如
String price ="30.0"。 -
为什么你的类型还是混淆了?
Detail.class在你的控制之下吗?如果是,请相应地进行调整。如果不是:为什么不先将它们包装成自己的类型?这样你就不需要经常转换和解析...... -
我添加了一个answer to your other question,您可以将价格转换功能添加到...
标签: collections java-8 java-stream