Arrays.stream() Vs Stream.of()

Arrays.stream() and Stream.of() both create streams in Java, but they have some differences in terms of usage, particularly with handling primitive arrays versus object arrays.

Here’s a detailed explanation of both with examples and use cases.


1. Arrays.stream()

  • Arrays.stream() is a method specifically designed to create a stream from an array.
  • It has overloaded methods that can handle both primitive arrays (int[]double[]long[]) and object arrays (Integer[]String[], etc.).
  • For primitive arrays, Arrays.stream() directly provides specialized streams (like IntStreamDoubleStreamLongStream) that avoid boxing overhead.

Example and Use Cases

Example 1: Using Arrays.stream() with a primitive array (int[]):

import java.util.Arrays;
import java.util.stream.IntStream;

public class ArraysStreamExample {
    public static void main(String[] args) {
        int[] intArray = {1, 2, 3, 4, 5};

        // Creating an IntStream directly from a primitive array
        IntStream intStream = Arrays.stream(intArray);
        intStream.forEach(System.out::println);
    }
}

Output:

1
2
3
4
5

Explanation:

  • Here, Arrays.stream(intArray) produces an IntStream directly from an int[] array. No boxing is needed, which makes it more efficient when working with primitives.

Example 2: Using Arrays.stream() with an object array (String[]):

import java.util.Arrays;
import java.util.stream.Stream;

public class ArraysStreamExample {
    public static void main(String[] args) {
        String[] strArray = {"apple", "banana", "cherry"};

        // Creating a Stream<String> from a String array
        Stream<String> strStream = Arrays.stream(strArray);
        strStream.forEach(System.out::println);
    }
}

Output:

apple
banana
cherry

When to Use Arrays.stream()

  • When working with primitive arrays (int[]double[]long[]), as it provides an optimized, specialized stream.
  • When you already have an array and want to create a stream for processing it.

2. Stream.of()

  • Stream.of() is a varargs method that can create a stream from individual values or from an array of objects.
  • It does not handle primitive arrays directly. If you pass a primitive array, Stream.of() will treat the entire array as a single element (creating a Stream<int[]> instead of an IntStream).
  • For object arrays (e.g., Integer[]String[]), Stream.of() works similarly to Arrays.stream() but can also take individual elements directly without requiring an array.

Example and Use Cases

Example 1: Using Stream.of() with an object array (Integer[]):

import java.util.stream.Stream;

public class StreamOfExample {
    public static void main(String[] args) {
        Integer[] intArray = {1, 2, 3, 4, 5};

        // Creating a Stream<Integer> from an Integer array
        Stream<Integer> intStream = Stream.of(intArray);
        intStream.forEach(System.out::println);
    }
}

Output:

1
2
3
4
5

Example 2: Using Stream.of() with individual values:

import java.util.stream.Stream;

public class StreamOfExample {
    public static void main(String[] args) {
        // Creating a Stream<String> directly from values
        Stream<String> strStream = Stream.of("apple", "banana", "cherry");
        strStream.forEach(System.out::println);
    }
}

Output:

apple
banana
cherry

Example 3: Passing a primitive array to Stream.of() (Notice the difference):

import java.util.stream.Stream;

public class StreamOfExample {
    public static void main(String[] args) {
        int[] intArray = {1, 2, 3, 4, 5};

        // This creates a Stream<int[]> rather than an IntStream
        Stream<int[]> streamOfArray = Stream.of(intArray);
        streamOfArray.forEach(arr -> System.out.println(Arrays.toString(arr)));
    }
}

Output:

[1, 2, 3, 4, 5]

Explanation:

  • Since Stream.of() does not handle primitive arrays, it treats intArray as a single element of type int[]. Hence, a Stream<int[]> is created instead of an IntStream.

When to Use Stream.of()

  • When you want to create a stream from individual elements rather than an array.
  • When you have an object array and want to create a stream.
  • Avoid using Stream.of() with primitive arrays directly, as it will treat the array as a single element. Use Arrays.stream() for primitive arrays instead.

Summary

FeatureArrays.stream()Stream.of()
Primitive arraysHandles directly, produces IntStream, etc.Not ideal, treats entire array as one element
Object arraysCreates Stream<T> from object arraysCreates Stream<T> from object arrays
Individual elementsNot applicableSupports direct input of individual values
Use caseWhen working with arrays (especially primitives)When you want flexibility with individual values or objects

Both methods are useful depending on the situation. Generally, use Arrays.stream() when dealing with arrays (especially primitives) and Stream.of() when you need flexibility with values or objects.

Post a Comment

Previous Post Next Post