Convert a String Stream to a String Array
In this step, we will create a stream of string values from a list and convert it into an array using the toArray()
method.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class StreamToArray {
public static void main(String[] args){
Stream<String> stream = List.of("UK", "US", "BR").stream();
// convert the string stream into a string array
String[] strArray = stream.toArray(String[]::new);
// print each element of the string array
for(String str : strArray) {
System.out.println(str);
}
// convert the string array into a stream again
Stream<String> stringStream = Arrays.stream(strArray);
// print each element of the stream
stringStream.forEach(System.out::println);
}
}
The output of the code will be as follows:
UK
US
BR
UK
US
BR