Java Arrays stream(long[] a)Method



Description

The Java Arrays stream(long[] a) method returns a LongStream covering all of the specified array.

Declaration

Following is the declaration for java.util.Arrays.stream(long[] a) method

public static LongStream stream​(long[] array)

Parameters

a − This is the array, assumed to be unmodified during use.

Return Value

This method returns a stream for the array elements.

Exception

NA

Java Arrays stream​(long[] a, int fromIndex, int toIndex) Method

Description

The Java Arrays stream(long[] a, int fromIndex, int toIndex) method returns a LongStream covering the specified range of the specified array.

Declaration

Following is the declaration for java.util.Arrays.stream(long[] a, int fromIndex, int toIndex) method

public static LongStream stream​(long[] array, int fromIndex, int toIndex)

Parameters

  • a − This is the array, assumed to be unmodified during use.

  • fromIndex − This is the index of the first element, inclusive, to be covered.

  • toIndex − This is the index of the last element, exclusive, to be covered

Return Value

This method returns a stream for the array elements.

Exception

  • ArrayIndexOutOfBoundsException − if fromIndex < 0 or toIndex > array.length

Getting stream of Array of longs Example

The following example shows the usage of Java Arrays stream(long[]) method. First, we've created an array of longs. Using stream() method, array is printed thereafter.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize array
      long arr[] = { 11L, 54L, 23L, 32L, 15L, 24L, 31L, 12L };

      System.out.print("Array: [ ");

      // use the stream to print each item
      Arrays.stream(arr).forEach(i -> System.out.print(i + " "));
      
      System.out.print("]");
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Array: [ 11 54 23 32 15 24 31 12 ]

Getting stream of Array of longs using Range Example

The following example shows the usage of Java Arrays stream(long[], int, int) method. First, we've created an array of longs. Using stream() method, array is printed thereafter.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize array
      long arr[] = { 11L, 54L, 23L, 32L, 15L, 24L, 31L, 12L };

      System.out.print("Array: [ ");

      // use the stream to print each item
      Arrays.stream(arr, 0, arr.length).forEach(i -> System.out.print(i + " "));
      
      System.out.print("]");
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Array: [ 11 54 23 32 15 24 31 12 ]

Getting stream of Sub-Array of longs Example

The following example shows the usage of Java Arrays stream(long[], int, int) method. First, we've created an array of longs. Using stream() method, a sub-array is printed thereafter.

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      // initialize array
      long arr[] = { 11L, 54L, 23L, 32L, 15L, 24L, 31L, 12L };

      System.out.print("Array: [ ");

      // use the stream to print each item
      Arrays.stream(arr, 0, arr.length).forEach(i -> System.out.print(i + " "));
      
      System.out.print("]");
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Array: [ 11 54 23 32 15 ]
java_util_arrays.htm
Advertisements