This post will discuss the difference between Stream.of() and Arrays.stream() method in Java.

The Stream.of() and Arrays.stream() are two commonly used methods for creating a sequential stream from a specified array. Both these methods return a Stream<T> when called with a non-primitive type T.

For instance, both Stream.of() and Arrays.stream() returns Stream<Integer> when called on an Integer array.

 
It is worth noting that Stream.of() method simply calls the Arrays.stream() method for non-primitive types as evident from the source code of Stream.of() method:

 
If Stream.of() is just a wrapper over the Arrays.stream() method, why is it even included in Java? The answer to this question is highlighted when we use a primitive array.

 
1. For primitives arrays, Arrays.stream() and Stream.of() have different return types. For example, if we pass a primitive integer array, the Stream.of() method returns Stream<int[]>, whereas Arrays.stream() returns an IntStream.

 
2. Since Stream.of() returns Stream<int[]> with integer array, we need to explicitly convert it into IntStream before consuming, as shown below:

 
3. Arrays.stream() method is overloaded for primitive arrays of int, long, and double type. For other primitive types, Arrays.stream() won’t work. It returns IntStream for an int[] array, LongStream for a long[] array and DoubleStream for a double[] array. On the other hand, Stream.of() has no overloaded method for primitive arrays. Instead, it has the following overloaded version which can take an object:

 
Since arrays are also objects in Java, the above method will be invoked when we pass a primitive array to Stream.of().

That’s all about the differences between Stream.of() and Arrays.stream() method in Java.