Last active
August 29, 2015 14:01
-
-
Save michael-simons/793f77e8b98feebe3984 to your computer and use it in GitHub Desktop.
Grouping map entries with Java 8 v2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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