Streams on Arrays in Java 8

Last Updated : 23 Jan, 2026

Java 8 introduced Streams, which provide a clean and efficient way to process collections and arrays. The stream() method of the Arrays class allows us to convert arrays into streams and perform operations like filtering, mapping, finding minimum/maximum, averaging, and more, using a declarative programming style.

Example

Java
import java.util.Arrays;
class GFG {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        Arrays.stream(arr)
              .forEach(System.out::print);
    }
}

Output
12345

Explanation:

  • Arrays.stream(arr) converts the array into an IntStream.
  • The forEach() method iterates over each element of the stream.

Syntax

public static IntStream stream(int[] array)

  • Parameters: array: The array to be converted into a stream
  • Returns: An IntStream (or LongStream, DoubleStream, Stream<T> based on array type)

Variations of Arrays.stream()

public static IntStream stream(int[] array)

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

public static LongStream stream(long[] array)

public static LongStream stream(long[] array, int startInclusive, int endExclusive)

public static DoubleStream stream(double[] array)

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

public static <T> Stream<T> stream(T[] array)

public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive)

Example 1: Average of Array Elements (Imperative vs Declarative)

This Java program calculates the average of array elements using both a traditional loop and Java 8 Streams, showing a simpler and more readable approach with streams.

Java
import java.util.Arrays;
class GFG {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9,10,
                     11,12,13,14,15,16,17,18,19,20};
        int sum = 0;
        for (int i = 0; i < arr.length; i++)
            sum += arr[i];
        System.out.println("Average using iteration : " + (sum / arr.length));
        sum = Arrays.stream(arr).sum();
        System.out.println("Average using streams : " + (sum / arr.length));
        System.out.print("Printing array elements : ");
        Arrays.stream(arr).forEach(e -> System.out.print(e + " "));
    }
}

Output
Average using iteration : 10
Average using streams : 10
Printing array elements : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 

Explanation

  • Arrays.stream(arr) converts the array into an IntStream
  • sum() is a terminal operation
  • forEach() iterates over stream elements

Example 2: Using Matching and Conversion Methods

This Java program demonstrates the use of Java 8 Streams on arrays to perform type conversions and conditional checks in a clean, declarative way using IntStream methods.

Java
import java.util.Arrays;
import java.util.function.IntPredicate;
class GFG {
    public static void main(String[] args) {

        int[] arr1 = {1,2,3,4,5,6,7,8,9,10};
        Arrays.stream(arr1)
              .asDoubleStream()
              .forEach(e -> System.out.print(e + " "));
        System.out.println();

        Arrays.stream(arr1)
              .asLongStream()
              .forEach(e -> System.out.print(e + " "));
        System.out.println();

        int[] arr2 = {1,2,3,4,5,11,13};
        IntPredicate predicate = e -> e % 11 == 0;
        System.out.println(Arrays.stream(arr2).anyMatch(predicate));

        int[] evenArr = {2,4,6,8};
        int[] oddArr = {1,3,5,7};
        System.out.println(Arrays.stream(evenArr).allMatch(e -> e % 2 == 0));
        System.out.println(Arrays.stream(oddArr).noneMatch(e -> e % 2 == 0));
    }
}

Output
1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 
1 2 3 4 5 6 7 8 9 10 
true
true
true

Explanation:

  • asDoubleStream() converts each int element to double, and asLongStream() converts them to long.
  • anyMatch() checks if any element in the array satisfies the given condition (divisible by 11).
  • allMatch() verifies whether all elements in the array are even.
  • noneMatch() checks that no element in the array satisfies the given condition.

Example 3: Aggregate Operations Returning Optional

This Java program shows how to use Java 8 Stream terminal operations to easily find the average, maximum, minimum, and sum of array elements.

Java
import java.util.Arrays;
class GFG {
    public static void main(String[] args) {
        int[] arr = {11,2,3,42,5,6,17,8,9,10};

        System.out.println(Arrays.stream(arr).average());
        System.out.println(Arrays.stream(arr).findAny());
        System.out.println(Arrays.stream(arr).findFirst());
        System.out.println(Arrays.stream(arr).max());
        System.out.println(Arrays.stream(arr).min());
        System.out.println(Arrays.stream(arr).reduce((x, y) -> x + y));
    }
}

Output
OptionalDouble[11.3]
OptionalInt[11]
OptionalInt[11]
OptionalInt[42]
OptionalInt[2]
OptionalInt[113]

Explanation:

  • average() returns the average of all elements as an OptionalDouble.
  • findAny() returns any element from the stream as an OptionalInt.
  • findFirst() returns the first element of the stream.
  • max() and min() find the maximum and minimum values in the array.
  • reduce((x, y) -> x + y) combines all elements to calculate their sum.

Example 4: Converting Optional to Primitive

This Java program demonstrates how to convert OptionalDouble and OptionalInt returned by Java 8 Stream operations into primitive values.

Java
import java.util.Arrays;
class GFG {
    public static void main(String[] args) {
        int[] arr = {11,2,3,42,5,6};
        double avg = Arrays.stream(arr).average().getAsDouble();
        int value = Arrays.stream(arr).findAny().getAsInt();
        System.out.println(avg);
        System.out.println(value);
    }
}

Output
11.5
11

Explanation:

  • average() returns an OptionalDouble, and getAsDouble() extracts the actual average value.
  • findAny() returns an OptionalInt, and getAsInt() retrieves an integer from it.
  • The program prints the calculated average and one element from the array.
Comment