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 to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources