Difference between Stream.of() and Arrays.stream() method in Java
Last Updated :
03 Mar, 2023
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
// 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 + " "));
}
}
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
// 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 + " "));
}
}
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
// Java program to demonstrate return type
// of Arrays.stream() and Stream.of() method
// for primitive arrays
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// Creating an integer array
int arr[] = { 1, 2, 3, 4, 5 };
// --------- Using Arrays.stream() ---------
// to convert int array into Stream
IntStream intStream = Arrays.stream(arr);
// Displaying elements in Stream
intStream.forEach(str -> System.out.print(str + " "));
// --------- Using Stream.of() ---------
// to convert int array into Stream
Stream<int[]> stream = Stream.of(arr);
// Displaying elements in Stream
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
// Java program to demonstrate need of flattening
// Stream.of() method returned type for primitive arrays
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// Creating an integer array
int arr[] = { 1, 2, 3, 4, 5 };
// --------- Using Arrays.stream() ---------
// to convert int array into Stream
IntStream intStream = Arrays.stream(arr);
// Displaying elements in Stream
intStream.forEach(str -> System.out.print(str + " "));
// --------- Using Stream.of() ---------
// to convert int array into Stream
Stream<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 IntStream
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
// Java program to demonstrate return type
// of Arrays.stream() method
// for primitive arrays of char
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// Creating a character array
char arr[] = { '1', '2', '3', '4', '5' };
// --------- Using Arrays.stream() ---------
// This will throw error
// to convert char array into Stream
Arrays.stream(arr);
}
}
Compilation Error in java code :- prog.java:20: error: no suitable method found for stream(char[]) Arrays.stream(arr); ^
Java
// Java program to demonstrate return type
// of Stream.of() method
// for primitive arrays of char
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// Creating a character array
char arr[] = { '1', '2', '3', '4', '5' };
// --------- Using Stream.of() ---------
// Will work efficiently
// to convert int array into Stream
Stream<char[]> stream = Stream.of(arr);
// Displaying elements in Stream
stream.forEach(str -> System.out.print(str + " "));
}
}
Output:
[C@548c4f57
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read