IntStream 介绍

IntStream 是一个原始整数值序列。该流提供了许多方法可以对该流中的元素顺序执行或并行执行一些聚合操作,比如 max() 或 average()。

使用示例

public class Test {
    public static void main(String[] args) throws Exception{
        //计算0到1000的累加和
        int reduce = IntStream.rangeClosed(0, 10000).parallel().reduce(0, (a, b) -> {
            return a + b;
        });
        System.out.println(reduce);
        List<Integer> lists = new ArrayList<>();
        lists.add(1);
        lists.add(2);
        lists.add(3);
        lists.add(4);
        lists.add(5);
        lists.add(6);
        //并行流
        Optional<Integer> sum = lists.parallelStream().reduce((a, b) -> a + b);
        System.out.println(sum.get());
    }
}

结果:
500500
21

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-09
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案