0

I want to call Collectors.summarizingInt on a set with Integers. Examples I have seen so far are on a Set with (say) Employees and are then called as collect(Collecters.summorizingInt(Employee::getWage)). For the bare Integers summorizingInt needs an argument so I can do collect(Collectors.summarizingInt((i) -> i)) but it feels a bit strange to provide a self mapper.

Are there alternatives?

dr jerry
  • 9,768
  • 24
  • 79
  • 122

2 Answers2

6

Another option could be combining mapToInt() to convert it to the IntStream and then call summaryStatistics() on it:

 IntSummaryStatistics summaryStatistics = Set.of(1, 3, 4)
                .stream()
                .mapToInt(Integer::intValue)
                .summaryStatistics();
kasptom
  • 2,363
  • 2
  • 16
  • 20
3
Set<Integer> integers;
integers.stream().collect(
  Collectors.summarizingInt(Integer::intValue));
Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40