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
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
String[] arr = { "Geeks", " for ", "Geeks" };
Stream<String> stream = Arrays.stream(arr);
stream.forEach(str -> System.out.print(str + " "));
}
}
|
Output:
Geeks for Geeks
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
import java.util.*;
import java.util.stream.Stream;
class GFG {
public static void main(String[] args)
{
Stream stream = Stream.of("Geeks", " for ", "Geeks");
stream.forEach(str -> System.out.print(str + " "));
}
}
|
Output:
Geeks for Geeks
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 clarifies 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
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
IntStream intStream = Arrays.stream(arr);
intStream.forEach(str -> System.out.print(str + " "));
Stream< int []> stream = Stream.of(arr);
stream.forEach(str -> System.out.print(str + " "));
}
}
|
Output:
1 2 3 4 5 [I@404b9385
- 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
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
IntStream intStream = Arrays.stream(arr);
intStream.forEach(str -> System.out.print(str + " "));
Stream< int []> stream = Stream.of(arr);
IntStream intStreamNew = stream.flatMapToInt(Arrays::stream);
intStreamNew.forEach(str -> System.out.print(str + " "));
}
}
|
Output:
1 2 3 4 5 1 2 3 4 5
- 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
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
char arr[] = { '1' , '2' , '3' , '4' , '5' };
Arrays.stream(arr);
}
}
|
Compilation Error in java code :- prog.java:20: error: no suitable method found for stream(char[]) Arrays.stream(arr); ^
Java
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
char arr[] = { '1' , '2' , '3' , '4' , '5' };
Stream< char []> stream = Stream.of(arr);
stream.forEach(str -> System.out.print(str + " "));
}
}
|
Output:
[C@548c4f57