The Java 8 Stream is a sequence of elements that supports sequential and parallel aggregate operations.
You can use the .stream().toArray()
function to convert a Java 8 stream to an array
In this tutorial, you will convert Java 8 Stream to an array.
Convert Stream to An Integer Array Using Method Reference
The Java 8 Stream also comes with primitive specifications like IntStream, LongStream and DoubleStream.
To convert a Stream to Integer Array,
- Convert the List to Stream of elements using
list.stream()
. - Invoke
stream.toArray()
to convert the stream of elements to an array with the generator function. - Pass Integer constructor reference as a generator function to create a new memory for the array.
Constructor reference way is easy to write and can be used for direct array creation without manipulating the elements in the stream.
Code
//main
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
List<Integer> numberList = Arrays.asList(1,5,4,3,6,7,8,3,5,7);
Integer[] intArray = numberList.stream().toArray(Integer[]::new);
for (int val:intArray) {
System.out.print(val+" ");
}
Output
1 5 4 3 6 7 8 3 5 7
Check this tutorial to reverse a stream, How to sort a Java8 stream in reverse order and then the reversed stream can be converted to an array.
Convert Stream to An Integer Array Using Map Function
To convert an Integer stream to an array,
- Use
intStream.map()
to iterate Integer stream of elements. - Pass a lambda function(
x -> x
) to transform the value. To double each element, pass the function asx- > (x*2)
. - Use
.toArray()
to convert the transformed value to an array.
Use this method to manipulate the value while converting the stream to an array.
Code
//main
import java.util.Arrays;
import java.util.stream.IntStream;
IntStream intStream = IntStream.of(5,3,7,2,4,7,1,2,6,8);
int[] intArray = intStream.map(x -> x*2 ).toArray();
Arrays.stream(intArray).forEach(value->{
System.out.print(value+" ");
});
Output
10 6 14 4 8 14 2 4 12 16
Convert Object Stream to Object Array
The Java Stream support both primitive and non-primitive datatypes.
To convert an Object Stream to an array,
- Use
stream.toArray()
to convert the stream to an array with an generator function. - Pass object constructor reference as generator function(
Student[]::new
).
Use this method for any non-primitive datatype conversion.
Code
//Student.java
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Student {
String name;
long id;
}
//main
import com.techmam.Student;
import java.util.Arrays;
import java.util.stream.Stream;
Stream<Student> studentStream = Stream.of(new Student("Musk",1),
new Student("Cook",3),
new Student("Pichai",2));
Student[] studentArr = studentStream.toArray(Student[]::new);
Arrays.stream(studentArr).forEach(student->{
System.out.println("id: "+student.getId()+", name: "+ student.getName());
});
Output
id: 1, name: Musk
id: 3, name: Cook
id: 2, name: Pichai
Convert String Stream to String Array using Lambda function
To convert a Stream of string elements to an array using the Lambda function,
- Use the
stream.toArray()
method to convert the stream to an array. - Pass lambda function(
size -> new String[size
) as generator function with size for string array creation.
Use constructor reference over the lambda function for simplicity, as both operate similarly.
Code
//main
import java.util.Arrays;
import java.util.stream.Stream;
Stream<String> stringStream = Stream.of("A","D","E","B","C");
String[] strArr = stringStream.toArray(size -> new String[size]);
Arrays.stream(strArr).forEach(System.out::print);
Output
ADEBC
Convert using Stream Collector
To convert a Stream of elements to an Array using a Stream collector,
- Convert the stream of elements to ArrayList using the
.collect()
method. - The collect method will create a new ArrayList using the
Collectors.toCollection(ArrayList::new)
method. - Use
arrayList.toArray()
with constructor reference(new Integer[integerList.size()]
), initialized with the size of the list to create an new array.
Use this approach when you want a stream as a List and perform some operation and convert it as an array, else this is the least preferred approach.
Code
//main
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Stream numberStream = Stream.of(1,2,3,4,5,6,7);
ArrayList integerList = (ArrayList) numberStream.collect(Collectors.toCollection(ArrayList::new));
Integer[] integerArr = (Integer[]) integerList.toArray(new Integer[integerList.size()]);
for (int value:integerArr ) {
System.out.print(value+" ");
}
Output
1 2 3 4 5 6 7