Skip to content

Instantly share code, notes, and snippets.

@seenimohamed
Last active June 18, 2019 10:17
Show Gist options
  • Save seenimohamed/c44c5df8d14c26fc4c949032e713d9f7 to your computer and use it in GitHub Desktop.
Save seenimohamed/c44c5df8d14c26fc4c949032e713d9f7 to your computer and use it in GitHub Desktop.
GroupBy functionalities
private String getPredictedModule(List<Result> result) {
Collector<Result, ?, Map<String, Double>> summarizeByModules =
Collectors.groupingBy(Result::getModule, Collectors.summingDouble(Result::getConfidence));
Map<String, Double> finalMap = new LinkedHashMap<>();
result.stream().collect(summarizeByModules)
.entrySet().stream().sorted(Map.Entry.<String, Double>comparingByValue()
.reversed()).forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue()));
return finalMap.keySet().stream().findFirst().get();
}
/**
Explanation :
1.Grouping a list by its keyString.
2.While grouping adding all the valueString based on key.
3.Sort by value in descending order.
4.Return the first key from map.
Collectors.groupingBy( groupBy param, Condition to groupby)
Collectors.summingDouble() -> will yield sum [Collectors.summarizingDouble() -> will yield stats, even more powerful fn]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment