Program to convert Primitive Array to Stream in Java

Java 8Object Oriented ProgrammingProgramming

To convert Primitive Array to Stream, you need to use the of() method.

Let’s say the following is our Primitive Array:

int[] myArr = new int[] { 20, 50, 70, 90, 100, 120, 150 };

Now, use the of() method to convert the primitive array to stream:

IntStream stream = IntStream.of(myArr);

The following is an example to convert primitive array to stream in Java:

Example

 Live Demo

import java.util.stream.IntStream;
import java.util.*;
public class Main {
   public static void main(String[] args) {
      int[] myArr = new int[] { 20, 50, 70, 90, 100, 120, 150 };
      System.out.println("The Primitive Array = "+Arrays.toString(myArr));
      IntStream stream = IntStream.of(myArr);
      System.out.println("Primitive Array to Stream = " + Arrays.toString(stream.toArray()));
   }
}

output

The Primitive Array = [20, 50, 70, 90, 100, 120, 150]
Primitive Array to Stream = [20, 50, 70, 90, 100, 120, 150]
raja
Published on 01-Apr-2019 12:30:18
Advertisements