Last active
March 21, 2021 16:36
-
-
Save mageddo/ec27af195598d6b856442bd0be66f952 to your computer and use it in GitHub Desktop.
Java Stream Bookmarks
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
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