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:
xxxxxxxxxx
1
String[] letters = new String[]{"A", "B", "C"};
2
3
// create stream from array
4
Stream<String> streamOfLetters = Arrays.stream(letters);
In this example, we use Arrays.stream()
to create a stream from the existing array.
xxxxxxxxxx
1
import java.util.Arrays;
2
import java.util.stream.Stream;
3
4
public class Example {
5
6
public static void main(String[] args) {
7
String[] letters = new String[]{"A", "B", "C"};
8
9
// create stream from array
10
Stream<String> streamOfLetters = Arrays.stream(letters);
11
12
// print elements
13
streamOfLetters.forEach(System.out::println);
14
}
15
}
Output:
xxxxxxxxxx
1
A
2
B
3
C