Java程序将int数组转换为IntStream

要将int数组转换为IntStream,让我们首先创建一个int数组:

int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

现在,创建IntStream并将上面的数组转换为IntStream:

IntStream stream = Arrays.stream(arr);

现在限制一些元素并找到流中这些元素的总和:

IntStream stream = Arrays.stream(arr);
stream = stream.limit(7);

System.out.println("Sum of first 7 elements = "+stream.sum());

以下是将int数组转换为IntStream的示例:

示例

import java.util.Arrays;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
      System.out.println("Array elements...");
      for (int res : arr)
      {
         System.out.println(res);
      }
      IntStream stream = Arrays.stream(arr);
      stream = stream.limit(7);
      System.out.println("Sum of first 7 elements = "+stream.sum());
   }
}

输出结果

Array elements...
10
20
30
40
50
60
70
80
90
100
Sum of first 7 elements = 280