Program to Convert Stream to an Array in Java
A Stream is a sequence of objects that support various methods which can be pipelined to produce the desired result.
An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case of objects of a class, the actual objects are stored in heap segment.
Below are various methods to convert Stream into an Array:
- Using toArray(): Stream provides toArray() method that returns an array containing the elements of the stream in the form of Object array.
Syntax:
stream.toArray()
Algorithm:
- Get the Stream
- Convert Stream into an Array using Stream.toArray() method.
- The obtained array is of type Object[]
- Return the Array Object[]
Program:
// Java Program to convert// Stream to array in Java 8importjava.util.*;importjava.util.stream.*;importjava.util.function.Function;classGFG {// Function to convert Stream to Arraypublicstatic<T> Object[] convertStreamToArray(Stream<T> stream){returnstream.toArray();}publicstaticvoidmain(String args[]){// Create a stream of integersStream<Integer> stream = Stream.of(1,2,3,4,5);// Convert Stream to arrayObject[] array = convertStreamToArray(stream);// Print the array of streamSystem.out.println("Array from Stream: "+ Arrays.toString(array));}}chevron_rightfilter_noneOutput:Array from Stream: [1, 2, 3, 4, 5]
- Using toArray(IntFunction generator): This method returns an array containing the elements of this stream, using the provided generator function to allocate the returned array, as well as any additional arrays that might be required for a partitioned execution or for resizing.
Syntax:
<A> A[] toArray(IntFunction<A[]> generator)
Algorithm:
- Get the Stream
- Convert Stream into an Array using Stream.toArray() method by passing Object[]::new as the generator function to allocate the returned array.
- The obtained array is of type Object[]
- Return the Array Object[]
Program:
// Java Program to convert// Stream to array in Java 8importjava.util.*;importjava.util.stream.*;importjava.util.function.Function;classGFG {// Function to convert Stream to Arraypublicstatic<T> Object[] convertStreamToArray(Stream<T> stream){returnstream.toArray(Object[] ::new);}publicstaticvoidmain(String args[]){// Create a stream of integersStream<Integer> stream = Stream.of(1,2,3,4,5);// Convert Stream to arrayObject[] array = convertStreamToArray(stream);// Print the array of streamSystem.out.println("Array from Stream: "+ Arrays.toString(array));}}chevron_rightfilter_noneOutput:Array from Stream: [1, 2, 3, 4, 5]
- Stream
to int[] using mapToInt() : Java 8 Stream API provides mapToInt() method that returns an IntStream consisting of the results of applying the given function to the elements of this stream. This is an intermediate operation. The obtained IntStream is then converted into int[] using toArray().Algorithm:
- Get the Stream of Integers
- Convert Stream into an IntStream using Stream.mapToInt() method.
- Convert the obtained IntStream into int[] using toArray()
- The obtained array is of type Integer
- Return the Array int[]
Program:
// Java Program to convert// Stream to array in Java 8importjava.util.*;importjava.util.stream.*;importjava.util.function.Function;classGFG {// Function to convert Stream to Arraypublicstaticint[] convertStreamToArray(Stream<Integer> stream){returnstream.mapToInt(Integer::intValue).toArray();}publicstaticvoidmain(String args[]){// Create a stream of integersStream<Integer> stream = Stream.of(1,2,3,4,5);// Convert Stream to arrayint[] array = convertStreamToArray(stream);// Print the array of streamSystem.out.println("Array of Integer from Stream: "+ Arrays.toString(array));}}chevron_rightfilter_noneOutput:Array of Integer from Stream: [1, 2, 3, 4, 5]
Recommended Posts:
- Program to convert Primitive Array to Stream in Java
- Program to convert Boxed Array to Stream in Java
- Program to convert a Map to a Stream in Java
- Program to convert a Set to Stream in Java using Generics
- Program to Convert List to Stream in Java
- Convert a Set to Stream in Java
- Convert Stream to Set in Java
- Convert an Iterable to Stream in Java
- Convert an Iterator to Stream in Java
- Program to convert Array to Set in Java
- Program to convert Array to List in Java
- Java Program to convert Character Array to IntStream
- Program to convert Byte Array to Writer in Java
- Program to Convert Set of Integer to Array of Integer in Java
- Program to Iterate over a Stream with Indices in Java 8
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

