繁体   English   中英

Java - groupingBy with collectingAndThen - 是否有更快/更好/更清洁的方式?

[英]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.

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