java中list集合stream流怎么把数据10个一分组

如题所述

java中list集合stream流把数据10个一分组步骤如下:
1、首先使用summingDouble和averagingDouble来实现DoublesummingScore=students.stream().collect(Collectors.summingDouble(Student:getScore));DoubleaveragingScore=students.stream().collect(Collectors.averagingDouble(Student::getScore));
2、使用summarizingDouble来实现它更为综合,可以直接计算出相关的汇总信息,DoubleSummaryStatisticssummarizingDouble=students.stream().collect(Collectors.summarizingDouble(Student::getScore))。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2023-04-25
这个是没有现成的收集器的,你可以用两种办法
第一种使用 stream 中的 reduce 进行收集 <U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator,BinaryOperator<U> combiner) 进行规约,并在 accumulator 中进行计算个数进行合并
第二种 使用 reactor 库的 buffer 操作符 进行操作

如 Flux.fromStream(lines).buffer(100) 将每个元素收集成为 100 个每组的list 进行处理
第二种方法比较好
相似回答
大家正在搜