Been working with Java streams quite a bit lately, and your current approach is actually quite solid! You’re already leveraging mapToInt(i -> i)
to create an IntStream
, which is perfect for summing. That said, you can tighten it up just a bit by replacing the lambda with a method reference—it makes the code a bit cleaner and easier to read:
int sum = integers.values().stream().mapToInt(Integer::intValue).sum();
This way, your intent is crystal clear, and you’re still fully taking advantage of the java stream sum
capabilities.