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/f10d29a09eabf141ed54 to your computer and use it in GitHub Desktop.
Save michael-simons/f10d29a09eabf141ed54 to your computer and use it in GitHub Desktop.
Grouping map entries with Java 8 v1
public static Map<LocalDate, Integer> summarizePeriodsOld(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(
HashMap::new,
(map, period) -> {
map.merge(period.getKey(), period.getValue(), (val1, val2) -> val1 + val2);
},
(map1, map2) -> {
map2.forEach((k, v) -> {
map1.merge(k, v, (val1, val2) -> val1 + val2);
});
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment