EN
Java 8 - create stream from existing array
0
points
In this article, we would like to show you how to create a stream from an existing array in Java 8.
Quick solution:
String[] letters = new String[]{"A", "B", "C"};
// create stream from array
Stream<String> streamOfLetters = Arrays.stream(letters);
Practical example
In this example, we use Arrays.stream()
to create a stream from the existing array.
import java.util.Arrays;
import java.util.stream.Stream;
public class Example {
public static void main(String[] args) {
String[] letters = new String[]{"A", "B", "C"};
// create stream from array
Stream<String> streamOfLetters = Arrays.stream(letters);
// print elements
streamOfLetters.forEach(System.out::println);
}
}
Output:
A
B
C