Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mageddo/ec27af195598d6b856442bd0be66f952 to your computer and use it in GitHub Desktop.
Save mageddo/ec27af195598d6b856442bd0be66f952 to your computer and use it in GitHub Desktop.
Java Stream Bookmarks
Stream.of(
new AbstractMap.SimpleEntry<>("Apple", 1),
new AbstractMap.SimpleEntry<>("Orange", 1),
new AbstractMap.SimpleEntry<>("Orange", 1),
new AbstractMap.SimpleEntry<>("Grape", 1),
new AbstractMap.SimpleEntry<>("Grape", 1),
new AbstractMap.SimpleEntry<>("Grape", 1)
)
.collect(
Collectors.groupingBy(
AbstractMap.SimpleEntry::getKey,
Collectors.reducing(
new AbstractMap.SimpleEntry<>("", 0),
(a, b) -> new AbstractMap.SimpleEntry<>(a.getKey(), a.getValue() + b.getValue())
)
)
)
.forEach((k, v) -> System.out.printf("k=%s, v=%s%n", k, v.getValue()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment