Open In App

Stream.of(T... values) in Java

Last Updated : 07 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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);
    }
}

Output
Geeks
for
Geeks

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);
    }
}

Output
5
7
9
12

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);
    }
}

Output
4
8
12
16
20

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);


Practice Tags :

Similar Reads