Difference between Stream.of() and Arrays.stream() method in Java
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.
|
1 2 3 |
Integer[] array = { 1, 2, 3, 4, 5 }; Stream.of(array) // return Stream<Integer> .forEach(System.out::println); // prints 1, 2, 3, … |
|
1 2 3 |
Integer[] array = { 1, 2, 3, 4, 5 }; Arrays.stream(array) // return Stream<Integer> .forEach(System.out::println); // prints 1, 2, 3, … |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * Returns a sequential ordered stream whose elements are the specified values. * * @param `<T>` The type of stream elements * @param `values` The elements of the new stream * @return the new stream */ @SafeVarargs @SuppressWarnings("varargs") // Creating a stream from an array is safe public static<T> Stream<T> of(T... values) { return Arrays.stream(values); } |
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.
|
1 2 3 |
int[] array = { 1, 2, 3, 4, 5 }; Arrays.stream(array) // returns IntStream .forEach(System.out::println); // prints 1, 2, 3, … |
|
1 2 3 |
int[] array = { 1, 2, 3, 4, 5 }; Stream.of(array) // returns `Stream<int[]>` .forEach(System.out::println); // prints [I@27d6c5e0 |
2. Since Stream.of() returns Stream<int[]> with integer array, we need to explicitly convert it into IntStream before consuming, as shown below:
|
1 2 3 4 |
int[] array = { 1, 2, 3, 4, 5 }; Stream.of(array) // returns `Stream<int[]>` .flatMapToInt(Arrays::stream) // returns IntStream .forEach(System.out::println); |
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:
|
1 2 3 4 5 6 7 8 9 10 |
/** * Returns a sequential {@code Stream} containing a single element. * * @param `t` a single element * @param `<T>` The type of stream elements * @return a singleton sequential stream */ public static<T> Stream<T> of(T t) { return StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false); } |
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.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)