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
Arraysclass and theDoubleStreaminterface. - Line 6: We create a stream of
doublevalues. - Line 7: We convert the stream to a
doubletype array using thetoArray()method. - Line 8: We print the double type array.