Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Program to convert Primitive Array to Stream in Java



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]
Updated on: 2019-07-30T22:30:25+05:30

246 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements