Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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