The stream(T[] array)
method of the Arrays
class in Java returns a sequential stream of the array passed as the argument. The parameter array
can be of any data type, T
, supported by Java. The stream
is a sequence of objects represented as a channel of data that has a source where the data is situated, and a destination where it is transmitted.
The stream
method has two overloaded types:
stream(T[] array)
stream(T[] array, int start, int end)
Let’s take a look at each of them below:
stream(T[] array)
This method simply takes an array and returns the sequential stream of all the elements of the array. See the syntax below:
The code snippet below illustrates the usage of the stream(T[] array)
method:
import java.util.*;import java.util.stream.*;class StreamDemo {public static void main( String args[] ) {String[] arr = { "I", "work", "at", "Educative", "Axis" };// storing a stream in a string objectStream<String> arr_stream = Arrays.stream(arr);// displaying each element in the stream objectarr_stream.forEach((ele) -> System.out.print(ele + " "));}}
stream(T[] array, int start, int end)
This method returns a sequential stream which only consists of a few specified elements from the array
. These specified elements are based on the range of the start
and end
indices passed to this method.
Have a look at the syntax below:
The code snippet below illustrates the usage of the stream(T[] array, int start, int end)
method:
import java.util.*;import java.util.stream.*;class StreamDemo {public static void main( String args[] ) {String[] arr = { "I", "work", "at", "Educative", "Axis" };// storing a stream of index 1, 2 and 3 in a string objectStream<String> arr_stream = Arrays.stream(arr, 1, 4);// displaying each element in the stream objectarr_stream.forEach((ele) -> System.out.print(ele + " "));}}
Free Resources