LongStream toArray() method in Java

Java 8Object Oriented ProgrammingProgramming

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]
raja
Updated on 30-Jul-2019 22:30:25

Advertisements