Java Stream toArray()

Learn to convert a Stream to an array using Stream toArray() API. In this totorial, we will see multiple examples for collecting the Stream elements into an array.

1. Stream toArray() Method

The toArray() method returns an array containing the elements of the given stream. This is a terminal operation.

Object[] toArray()
<T> T[]  toArray(IntFunction<T[]> generator)

toArray() method is an overloaded method. The second method uses a generator function to allocate the returned array.

The generator function takes an integer, which is the size of the desired array and produces an array of the desired size.

2. Stream toArray() Example

Example 1: Converting Stream of String to Array of String

In the given example, we are converting a stream to an array using using toArray() API.

Stream<String> tokenStream = Arrays.asList("A", "B", "C", "D").stream();	//stream
		
String[] tokenArray = tokenStream.toArray(String[]::new); 	//array

System.out.println(Arrays.toString(tokenArray));	

Program output.

[A, B, C, D]

Example 2: Converting an Infinite Stream to an Array

To convert an infinite stream into array, we must limit the stream to a finite number of elements.

Infinite stream of Integers
IntStream infiniteNumberStream = IntStream.iterate(1, i -> i+1);
		
int[] intArray = infiniteNumberStream.limit(10)
			.toArray();

System.out.println(Arrays.toString(intArray));	

Program output.

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Infinite boxed stream of Integers

IntStream infiniteNumberStream = IntStream.iterate(1, i -> i+1);

Integer[] integerArray = infiniteNumberStream.limit(10)
					.boxed()
					.toArray(Integer[]::new);

System.out.println(Arrays.toString(integerArray));

Program output.

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Example 3: Stream filter and collect to an Array

Sometimes we need to find specific items in stream and then add only those elements to array. Here, we can use Stream.filter() method to pass a predicate which will return only those elements who match the pre-condition.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main 
{
	public static void main(String[] args) 
	{
		List<Employee> employeeList = new ArrayList<>(Arrays.asList(
							new Employee(1, "A", 100),
							new Employee(2, "B", 200),
							new Employee(3, "C", 300),
							new Employee(4, "D", 400),
							new Employee(5, "E", 500),
							new Employee(6, "F", 600)));
		
		Employee[] employeesArray = employeeList.stream()
						.filter(e -> e.getSalary() < 400)
						.toArray(Employee[]::new);
		
		System.out.println(Arrays.toString(employeesArray));
	}
}

Program output.

[Employee [id=1, name=A, salary=100.0], 
Employee [id=2, name=B, salary=200.0], 
Employee [id=3, name=C, salary=300.0]]

3. Conclusion

We can use Stream toArray() function is variety of ways to collect stream elements into an array in all usescases.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.