![](/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.