Java Utility Library

Java Arrays - stream() Method



The java.util.Arrays.stream() method returns a sequential DoubleStream with the specified range of the specified array as its source.

Syntax

public static DoubleStream stream(double[] array,
                                  int startInclusive,
                                  int endExclusive)

Parameters

array Specify the array, assumed to be unmodified during use.
startInclusive Specify the first index to cover, inclusive.
endExclusive Specify index immediately past the last index to cover.

Return Value

Returns a DoubleStream for the array range.

Exception

Throws ArrayIndexOutOfBoundsException, if startInclusive is negative, endExclusive is less than startInclusive, or endExclusive is greater than the array size.

Example:

In the example below, the java.util.Arrays.stream() method returns a sequential DoubleStream with the specified range of the given array as its source.

import java.util.*;
import java.util.stream.*; 

public class MyClass {
  public static void main(String[] args) {
    //creating a double array
    double Arr[] = {1, 2, 3, 4, 5};

    //creating DoubleStream object by converting the
    //given range [2,5) of the array into stream 
    DoubleStream stream = Arrays.stream(Arr, 2, 5); 

    //printing the stream
    System.out.print("The stream contains: "); 
    stream.forEach(str -> System.out.print(str + " "));  
  }
}

The output of the above code will be:

The stream contains: 3.0 4.0 5.0 

❮ Java.util - Arrays