没有映射器的java流收集器?

dr *_*rry 0 java stream summarize

我想Collectors.summarizingInt用整数调用一个集合。到目前为止我看到的例子是在一个带有(比如)Employees 的 Set 上,然后被称为collect(Collecters.summorizingInt(Employee::getWage)). 对于裸整数 summorizingInt 需要一个参数,所以我可以这样做,collect(Collectors.summarizingInt((i) -> i))但提供一个自映射器感觉有点奇怪。

有替代品吗?

kas*_*tom 6

另一种选择是结合mapToInt()将其转换为IntStream,然后在其上调用summaryStatistics()

 IntSummaryStatistics summaryStatistics = Set.of(1, 3, 4)
                .stream()
                .mapToInt(Integer::intValue)
                .summaryStatistics();
Run Code Online (Sandbox Code Playgroud)