Last Updated : 07 May, 2025
Stream of(T... values) returns a sequential ordered stream whose elements are the specified values. A sequential stream works like a for-loop using a single core. On the other hand, a Parallel stream divides the provided task into many tasks and runs them in different threads, utilizing multiple cores of the computer. To create a parallel stream, we need to call parallel() explicitly on the stream.
Declaration of Stream.of(T... values)The declaration is listed below:
static <T> Stream<T> of(T... values)
where,
Important Points:
Now, we are going to discuss some examples for better understanding
Examples of Stream.of(T... values)Example 1: Stream of Strings
Java
// Java program for Stream.of()
// to get sequential ordered stream
import java.util.stream.Stream;
class Geeks {
// Driver code
public static void main(String[] args)
{
// creating a stream of Strings
// and printing sequential
// ordered stream
Stream<String> s = Stream.of("Geeks", "for", "Geeks");
s.forEach(System.out::println);
}
}
Explanation: We are creating a stream of three strings using stream.of() method and then it processess the stream sequentially and then printing each element using the forEach().
Example 2: Stream of Integers
Java
// Java program for Stream.of()
// to get sequential ordered stream
import java.util.stream.Stream;
class Geeks {
// Driver code
public static void main(String[] args)
{
// creating a stream of Integer
// and printing sequential
// ordered stream
Stream<Integer> s = Stream.of(5, 7, 9, 12);
s.forEach(System.out::println);
}
}
Explanation: We are creating a stream of integers using stream.of() method and then it process the stream sequentially and then printing each element using the forEach().
Example 3: Stream of Long Primitives
Java
// Java program for Stream.of()
// to get sequential ordered stream
import java.util.stream.Stream;
class Geeks {
// Driver code
public static void main(String[] args)
{
// creating a stream of Long
// and printing sequential
// ordered stream
Stream<Long> s = Stream.of(4L, 8L, 12L, 16L, 20L);
s.forEach(System.out::println);
}
}
Explanation: We are creating a stream of Long values using stream.of() method and then it process the stream sequentially and then printing each element using the forEach().
Advantages of using StreamsThe advantages of streams are listed below:
Note: If we want to use a parallel stream, we can invoke .parallel() method after creating the stream like this:
stream.parallel().forEach(System.out::println);
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4