并行流处理大规模数据集与信息检索搜索工具
并行流统计方法
在数据处理中,我们经常需要对大量数据进行统计分析。例如,统计年龄在 25 到 50 岁之间的人数。以下是实现该功能的代码:
public static void peopleBetween25and50(List<Record> records) {
System.out.println("****************************************");
System.out.println("People between 25 and 50");
int count = records.parallelStream()
.map(r -> r.getAge())
.filter(a -> (a >= 25) && (a <= 50))
.mapToInt(a -> 1)
.reduce(0, Integer::sum);
System.out.println("People between 25 and 50: " + count);
System.out.println("****************************************");
}
该方法接收一个 Record
对象列表作为输入参数,使用 parallelStream()
方法获取并行流,然后通过一系列操作统计年龄在 25 到 50 岁之间