Java – groupingby with collectingandthen – is there a faster / better / cleaner way?
•
Java
I have the following courses (getters):
public class AlgorithmPrediction {
private final String algorithmName;
private final Map<BaseDatabaseProduct,Double> productsToAccuracy;
}
Now I want to create a mapping from the collection populated by the algorithmprediction object, with algorithmname (unique) as the key and productstoaccuracy as the value I can't think of anything more complicated 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 did I miss? thank you!
Solution
algorithmPredictions.stream()
algorithmPredictions.stream()
.collect(toMap(AlgorithmPrediction::getAlgorithmName,AlgorithmPrediction::getProductsToAccuracy));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
