LongStream toArray() method in Java


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

The syntax is as follows.

long[] toArray()

To use the LongStream class in Java, import the following package.

import java.util.stream.LongStream;

Create LongStream and add some elements.

LongStream longStream = LongStream.of(25000L, 28999L, 6767788L);

Create a Long array and use the toArray() method to return the elements of the stream as array elements.

long[] myArr = longStream.toArray();

The following is an example to implement LongStream toArray() method in Java.

Example

 Live Demo

import java.util.*;
import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(25000L, 28999L, 6767788L);
      long[] myArr = longStream.toArray();
      System.out.println("The elements of the array: "+Arrays.toString(myArr));
   }
}

Output

The elements of the array: [25000, 28999, 6767788]

Updated on: 30-Jul-2019

81 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements