EN
Java 8 - create stream from part of array
0 points
In this article, we would like to show you how to create a stream from part of the array in Java 8.
Quick solution:
xxxxxxxxxx
1
String[] letters = new String[]{"A", "B", "C", "D"};
2
3
// create stream from part array between indexes 2-4
4
Stream<String> streamOfLetters = Arrays.stream(letters, 2, 4);
In this example, we use Arrays.stream()
to create a stream from part of the array.
Syntax:
xxxxxxxxxx
1
Arrays.stream(arrayName, indexStart, indexEnd);
Code example:
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", "D"};
8
9
// create stream from part array between indexes 2-4
10
Stream<String> streamOfLetters = Arrays.stream(letters, 2, 4);
11
12
// print elements
13
streamOfLetters.forEach(System.out::println);
14
}
15
}
Output:
xxxxxxxxxx
1
C
2
D