Java Streams - Stream toArray() example








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

Syntax

toArray has the following syntax.

Object[] toArray()

Example

The following example shows how to use toArray.

import java.util.Arrays;
import java.util.List;
//from w  w  w .  ja v  a2s  .  com
public class Main {
  public static void main(String[] args) {
    List<String> stringList = Arrays.asList("2","1","3","4");

    Object[] n = stringList.stream()      
              .toArray();
    System.out.println(Arrays.toString(n));
  }
}

The code above generates the following result.