Stream.of(T... values) in Java
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,
- Stream: It is an interface.
- T: It is a type of stream element.
- values: It represents the elements of the new stream, and the function returns the new stream.
Important Points:
- This method creates a stream from a set of values. This method is useful when we have a small number of items and do not want the extra work of making a collection.
- By default, the stream processes the items one by one. If we want it to run in parallel, we can simply call .parallel() after Stream.of()
- Parallel stream speeds up the processing for large and complex tasks, but it slows things down for small and simpler tasks.
- Streams are lazy. It means that the operations like filtering or mapping elements are not performed until a terminal operation is invoked.
- With the help of streams, we can perform various operations, which makes it a powerful tool for handling collections in Java.
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 Streams
The advantages of streams are listed below:
- It provide a functional approach.
- It makes the code more concise and also increases the readability, especially for operations like fitering, transforming and aggregating data.
- Stream works well with lambda expressions.
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);
Similar Reads
Stream.of(T... values) in Java with examples 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 cor
3 min read
Stream In Java Stream was introduced in Java 8, the Stream API is used to process collections of objects. A stream in Java is a sequence of objects that supports various methods that can be pipelined to produce the desired result. Use of Stream in JavaThe uses of Stream in Java are mentioned below:Stream API is a
7 min read
Stream of() method in Java Stream of(T t) Stream of(T t) returns a sequential Stream containing a single element. Syntax : static Stream of(T t) Parameters: This method accepts a mandatory parameter t which is the single element in the Stream. Return Value: Stream of(T t) returns a sequential Stream containing the single spec
2 min read
Stream sorted() in Java Stream sorted() returns a stream consisting of the elements of this stream, sorted according to natural order. For ordered streams, the sort method is stable but for unordered streams, no stability is guaranteed. It is a stateful intermediate operation i.e, it may incorporate state from previously s
2 min read
Java 8 Stream Tutorial Java 8 introduces Stream, which is a new abstract layer, and some new additional packages in Java 8 called java.util.stream. A Stream is a sequence of components that can be processed sequentially. These packages include classes, interfaces, and enum to allow functional-style operations on the eleme
15+ min read
Stream.of(T t) in Java with examples Stream of(T t) returns a sequential Stream containing a single element i.e, a singleton sequential stream. A sequential stream work just like for-loop using a single core. On the other hand, a Parallel stream divide the provided task into many and run them in different threads, utilizing multiple co
2 min read
Array to Stream in Java Prerequisite : Stream In Java Using Arrays.stream() : Syntax : public static <T> Stream<T> getStream(T[] arr) { return Arrays.stream(arr); } where, T represents generic type. Example 1 : Arrays.stream() to convert string array to stream. Java // Java code for converting string array // t
3 min read
Streams on Arrays in Java 8 In this article, we would be going through stream method of Arrays class which is added in Java 8, it simplifies many operations on arrays as well have improved the efficiency. Addition of different features like lambdas and streams in java 8 have made java efficient to write elegant code which have
7 min read
10 Ways to Create a Stream in Java The Stream API, introduced in Java 8, it is used to process collections of objects. Stream is a sequence of objects, that supports many different methods which can be pipe lined to produce the desired result. The features of Java stream are â A stream is not a data structure alternatively it takes i
9 min read
Convert Stream to Set in Java Below given are some methods which can be used to convert Stream to Set in Java. Method 1 : Using Collectors Stream collect() method takes elements from a stream and stores them in a collection.collect(Collector.toSet()) collects elements from a stream to a Set. Stream.collect() method can be used t
3 min read