![](/img/trans.png)
[英]Is there a better/cleaner way to conditionally create a type than using instanceof? [Java]
[英]Java - groupingBy with collectingAndThen - Is there a faster / better / cleaner way?
我有以下课程(有getter):
public class AlgorithmPrediction {
private final String algorithmName;
private final Map<BaseDatabaseProduct, Double> productsToAccuracy;
}
现在我想从AlgorithmPrediction
对象填充的集合中创建一个映射,其中algorithmName
(唯一)作为键, productsToAccuracy
作为值。 我想不出比这更复杂的东西:
algorithmPredictions.stream()
.collect(
groupingBy(
AlgorithmPrediction::getAlgorithmName,
Collectors.collectingAndThen(
toSet(),
s -> s.stream().map(AlgorithmPrediction::getProductsToAccuracy).collect(toSet())
)
)
);
这不可能是对的。 我错过了什么? 谢谢!
algorithmPredictions.stream()
.collect(toMap(AlgorithmPrediction::getAlgorithmName,
AlgorithmPrediction::getProductsToAccuracy));
如果我理解正确,你不能使用Collectors.toMap(Function<> keyMapper, Function<> valueMapper)
收集器,如下所示:
Map<String, Map<BaseDatabaseProduct, Double>> result = algorithmPredictions.stream()
.collect(Collectors.toMap(
AlgorithmPrediction::getAlgorithmName,
AlgorithmPrediction::getProductsToAccuracy));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.