6

我有以下课程(带有吸气剂):

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())
                )
        )
);

这不可能。我错过了什么?谢谢!

4

2 回答 2

6
algorithmPredictions.stream()
                    .collect(toMap(AlgorithmPrediction::getAlgorithmName, 
                                   AlgorithmPrediction::getProductsToAccuracy));
于 2015-08-26T09:13:35.513 回答
4

如果我对您的理解正确,您不能使用Collectors.toMap(Function<> keyMapper, Function<> valueMapper)收集器,如下所示:

Map<String, Map<BaseDatabaseProduct, Double>> result = algorithmPredictions.stream()
        .collect(Collectors.toMap(
                AlgorithmPrediction::getAlgorithmName,
                AlgorithmPrediction::getProductsToAccuracy));
于 2015-08-26T09:14:41.173 回答