Java Streams - IntStream toArray() example








IntStream toArray() returns an array containing the elements of this stream. This is a terminal operation.

Syntax

toArray has the following syntax.

int[] toArray()

Example

The following example shows how to use toArray.

import java.util.stream.IntStream;
//from  www  .  ja  va2  s  . co m
public class Main {
  public static void main(String[] args) {
    IntStream i = IntStream.of(6,5,7,1, 2, 3, 3);
    int[] v = i.toArray();
    for(int n:v){
      System.out.println(n);  
    }
  }
}

The code above generates the following result.