Java Program to convert int array to IntStream


To convert int array to IntStream, let us first create an int array:

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

Now, create IntStream and convert the above array to IntStream:

IntStream stream = Arrays.stream(arr);

Now limit some elements and find the sum of those elements in the stream:

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

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

The following is an example to convert int array to IntStream:

Example

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());
   }
}

Output

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

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements