September 2019
Intermediate to advanced
816 pages
18h 47m
English
Once we create a Stream from an array, we have access to all the Stream API goodies. Therefore, this is a handy operation that is important to have in our tool belt.
Let's start with an array of strings (can be other objects as well):
String[] arr = {"One", "Two", "Three", "Four", "Five"};
The easiest way to create Stream from this String[] array is to rely on the Arrays.stream() method available starting with JDK 8:
Stream<String> stream = Arrays.stream(arr);
Or, if we need a stream from a sub-array, then simply add the range as arguments. For example, let's create a Stream from the elements that range between (0,2), which are one and two:
Stream<String> stream = Arrays.stream(arr, 0, 2);
The same cases, ...