Skip to content

Instantly share code, notes, and snippets.

@michael-simons
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michael-simons/793f77e8b98feebe3984 to your computer and use it in GitHub Desktop.
Save michael-simons/793f77e8b98feebe3984 to your computer and use it in GitHub Desktop.
Grouping map entries with Java 8 v2
public static Map<LocalDate, Integer> summarizePeriods(final List<Bike> bikes, final Predicate<Map.Entry<LocalDate, Integer>> entryFilter) {
return bikes.stream()
.filter(Bike::hasMilages)
.flatMap(bike -> bike.getPeriods().entrySet().stream())
.filter(Optional.ofNullable(entryFilter).orElse(entry -> true))
.collect(
Collectors.groupingBy(
Map.Entry::getKey,
Collectors.reducing(0, Map.Entry::getValue, Integer::sum)
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment