What is the toArray() method of the DoubleStream interface?

The DoubleStream interface’s toArray() method creates an array of the double type. This array consists of the stream’s elements. This method is a terminal/last operation in the stream pipeline.

Syntax

double[] toArray();

Parameters

The method has no parameters.

Return value

The method returns an array of the double type.

Code

import java.util.Arrays;
import java.util.stream.DoubleStream;
class Main {
public static void main(String[] args) {
DoubleStream doubleStream = DoubleStream.of(5.6, 10.9, 12.4, 34.2, 0.00032);
double[] doubleArray = doubleStream.toArray();
System.out.println("Converting the DoubleStream to an array - " + Arrays.toString(doubleArray));
}
}

Explanation

  • Lines 1 and 2: We import the Arrays class and the DoubleStream interface.
  • Line 6: We create a stream of double values.
  • Line 7: We convert the stream to a double type array using the toArray() method.
  • Line 8: We print the double type array.
New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources