Difference between Stream.of() and Arrays.stream() method in Java
Arrays.stream()
The stream(T[] array) method of Arrays class in Java, is used to get a Sequential Stream from the array passed as the parameter with its elements. It returns a sequential Stream with the elements of the array, passed as parameter, as its source.
Example:
// Java program to demonstrate Arrays.stream() method import java.util.*; import java.util.stream.*; class GFG { public static void main(String[] args) { // Creating a String array String[] arr = { "Geeks", "for", "Geeks" }; // Using Arrays.stream() to convert // array into Stream Stream<String> stream = Arrays.stream(arr); // Displaying elements in Stream stream.forEach(str -> System.out.print(str + " ")); } } |
Stream.of()
The Stream of(T… values) returns a sequential ordered stream whose elements are the specified values. Stream.of() method simply calls the Arrays.stream() method for non-primitive types.
Example:
// Java code for Stream of(T... values) // to get a sequential ordered stream whose // elements are the specified values. import java.util.*; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // Creating an Stream Stream stream = Stream.of("Geeks", "for", "Geeks"); // Displaying the sequential ordered stream stream.forEach(str -> System.out.print(str + " ")); } } |
These both methods are the two most commonly used methods for creating a sequential stream from a specified array. Both these methods returns a Stream<T> when called with a non-primitive type T.
Difference between Arrays.stream() and Stream.of()
Even if Stream.of() is a wrapper over the Arrays.stream() method, there are certain point of differences which clearifies as when to use a Arrays.stream() or when to use Stream.of(). Below are some of the differences between the above two stated methods:
- Different return types:
For primitives arrays (like int[], long[] etc), Arrays.stream() and Stream.of() have different return types.
Example: Passing an integer array, the Stream.of() method returns Stream
whereas Arrays.stream() returns an IntStream. // Java program to demonstrate return type// of Arrays.stream() and Stream.of() method// for primitive arraysimportjava.util.*;importjava.util.stream.*;classGFG {publicstaticvoidmain(String[] args){// Creating an integer arrayintarr[] = {1,2,3,4,5};// --------- Using Arrays.stream() ---------// to convert int array into StreamIntStream intStream = Arrays.stream(arr);// Displaying elements in StreamintStream.forEach(str -> System.out.print(str +" "));// --------- Using Stream.of() ---------// to convert int array into StreamStream<int[]> stream = Stream.of(arr);// Displaying elements in Streamstream.forEach(str -> System.out.print(str +" "));}}chevron_rightfilter_none - Stream.of() needs flattening whereas Arrays.stream() does not:
As the ideal class used for processing of Streams of primitive types are their primitive Stream types (like IntStream, LongStream, etc). Therefore Stream.of() needs to be explicitly flattened into its primitive Stream before consuming.
Example:
// Java program to demonstrate need of flattenning// Stream.of() method returned type for primitive arraysimportjava.util.*;importjava.util.stream.*;classGFG {publicstaticvoidmain(String[] args){// Creating an integer arrayintarr[] = {1,2,3,4,5};// --------- Using Arrays.stream() ---------// to convert int array into StreamIntStream intStream = Arrays.stream(arr);// Displaying elements in StreamintStream.forEach(str -> System.out.print(str +" "));// --------- Using Stream.of() ---------// to convert int array into StreamStream<int[]> stream = Stream.of(arr);// ***** Flattening of Stream<int[]> into IntStream *****// flattenning Stream<int[]> into IntStream// using flatMapToInt()IntStream intStreamNew = stream.flatMapToInt(Arrays::stream);// Displaying elements in IntStreamintStreamNew.forEach(str -> System.out.print(str +" "));}}chevron_rightfilter_none - Stream.of() is generic whereas Arrays.stream is not:
Arrays.stream() method only works for primitive arrays of int[], long[], and double[] type, and returns IntStream, LongStream and DoubleStream respectively. For other primitive types, Arrays.stream() won’t work.
On the other hand, Stream.of() returns a generic Stream of type T (Stream). Hence, it can be used with any type. Example:
- For Arrays.stream() method:
// Java program to demonstrate return type// of Arrays.stream() method// for primitive arrays of charimportjava.util.*;importjava.util.stream.*;classGFG {publicstaticvoidmain(String[] args){// Creating a character arraychararr[] = {'1','2','3','4','5'};// --------- Using Arrays.stream() ---------// This will throw error// to convert char array into StreamArrays.stream(arr);}}chevron_rightfilter_noneOutput:
Compilation Error in java code :-
prog.java:20: error: no suitable method found for stream(char[])
Arrays.stream(arr);
^ - For Stream.of() method:
// Java program to demonstrate return type// of Stream.of() method// for primitive arrays of charimportjava.util.*;importjava.util.stream.*;classGFG {publicstaticvoidmain(String[] args){// Creating a character arraychararr[] = {'1','2','3','4','5'};// --------- Using Stream.of() ---------// Will work efficiently// to convert int array into StreamStream<char[]> stream = Stream.of(arr);// Displaying elements in Streamstream.forEach(str -> System.out.print(str +" "));}}chevron_rightfilter_none
- For Arrays.stream() method:
Recommended Posts:
- Difference between == and .equals() method in Java
- Difference between static and non-static method in Java
- Difference between x++ and x=x+1 in Java
- Difference between Java and JavaScript
- Difference between super() and this() in java
- Difference between while and do-while loop in C, C++, Java
- Difference between Java and C language
- Difference between Groovy and Java
- Difference between concat() and + operator in Java
- Difference between throw and throws in Java
- Difference between notify() and notifyAll() in Java
- Difference between ArrayList and HashSet in Java
- Difference between an Iterator and ListIterator in Java
- Difference between Arrays and Collection in Java
- Difference Between LinkedList and LinkedHashSet in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



