Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
longstream

What is the toArray() method of the LongStream interface?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The toArray() method of the LongStream interface is used to create a long type array that consists of the elements in the stream. This method is a terminal/last operation in the stream pipeline.

Syntax

long[] toArray();

Parameters

This method has no parameters.

Return value

This method returns a long type array.

Code

import java.util.Arrays;
import java.util.stream.LongStream;
class Main {
public static void main(String[] args) {
LongStream longStream = LongStream.range(5, 10);
long[] longArray = longStream.toArray();
System.out.println("Converting the longStream to an array - " + Arrays.toString(longArray));
}
}
Implementation of the toArray method

Explanation

  • In lines 1 and 2, we import the Arrays class and the LongStream interface.
  • In line 6, we create a stream of long values, using the range() method of the LongStream interface.
  • In line 7, we convert the stream to a long array, using the toArray() method.
  • In line 8, we print the object array.

RELATED TAGS

java
communitycreator
longstream

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring