Stream toArray() example

Description

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;
/*  w ww.jav a 2s  .c o  m*/
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.





















Home »
  Java Streams »
    Reference »




Collectors
DoubleStream
DoubleStream.Builder
IntStream
IntStream.Builder
LongStream
LongStream.Builder
Stream
Stream.Builder