Skip to content

Instantly share code, notes, and snippets.

@GayanM
Created September 25, 2022 10:28
Show Gist options
  • Select an option

  • Save GayanM/96b1da4fe1c2d370b39939ebcc67697c to your computer and use it in GitHub Desktop.

Select an option

Save GayanM/96b1da4fe1c2d370b39939ebcc67697c to your computer and use it in GitHub Desktop.
Obtain a collection of statistic figures (i.e. sum, average, max, min, count) for all products of given category
@RequestMapping("/statistics")
public DoubleSummaryStatistics getStatistics(@RequestParam("category") final Optional<String> category) {
return productRepos.findAll().stream().filter(p -> category.get().equalsIgnoreCase(p.getCategory())).
mapToDouble(p -> p.getPrice()).summaryStatistics();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment