DoubleStream toArray() method in Java


The toArray() method returns an array containing the elements of this stream.

The syntax is as follows:

double[] toArray()

To use the DoubleStream class in Java, import the following package:

import java.util.stream.DoubleStream;

Create DoubleStream and add some elements:

DoubleStream doubleStream = DoubleStream.of(20.7, 30.5, 45.9, 60.2, 75.9, 88.3, 95.2);

Now, use the toArray() method to return an array with the stream elements:

double[] myArr = doubleStream.toArray();

The following is an example to implement DoubleStream toArray() method in Java:

Example

 Live Demo

import java.util.*;
import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      DoubleStream doubleStream = DoubleStream.of(20.7, 30.5, 45.9, 60.2, 75.9, 88.3, 95.2);
      double[] myArr = doubleStream.toArray();
      System.out.println("Array...
"+Arrays.toString(myArr));    } }

Output

Array...
[20.7, 30.5, 45.9, 60.2, 75.9, 88.3, 95.2]

Updated on: 30-Jul-2019

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements