I have the following class (with getters):
public class AlgorithmPrediction {
private final String algorithmName;
private final Map<BaseDatabaseProduct, Double> productsToAccuracy;
}
Now I want to create a map from a set filled by AlgorithmPrediction
objects with the algorithmName
(unique) as key and productsToAccuracy
as value. I couldn't come up with anything less complex than this:
algorithmPredictions.stream()
.collect(
groupingBy(
AlgorithmPrediction::getAlgorithmName,
Collectors.collectingAndThen(
toSet(),
s -> s.stream().map(AlgorithmPrediction::getProductsToAccuracy).collect(toSet())
)
)
);
This can't be right. What am I missing? Thank you!
algorithmPredictions.stream()
.collect(toMap(AlgorithmPrediction::getAlgorithmName,
AlgorithmPrediction::getProductsToAccuracy));
If I've understood you correctly, couldn't you use Collectors.toMap(Function<> keyMapper, Function<> valueMapper)
collector, like the following:
Map<String, Map<BaseDatabaseProduct, Double>> result = algorithmPredictions.stream()
.collect(Collectors.toMap(
AlgorithmPrediction::getAlgorithmName,
AlgorithmPrediction::getProductsToAccuracy));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With