IntStream toArray() method in Java


The toArray() method returns an array containing the elements of this stream.

Set the elements in the stream with the IntStream class of() method.

IntStream stream = IntStream.of(20, 40, 60, 70, 100, 120, 140);

Now, display an array with the elements of this stream using the toArray() method.

int[] myArr = stream.toArray();

The following is the syntax.

int[] toArray()

The following is an example to implement IntStream toArray() method in Java.

Example

 Live Demo

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream stream = IntStream.of(20, 40, 60, 70, 100, 120, 140);
      int[] myArr = stream.toArray();
      System.out.println(Arrays.toString(myArr));
   }
}

Output

[20, 40, 60, 70, 100, 120, 140]

Updated on: 30-Jul-2019

203 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements