Open In App

Stream.of(T… values) in Java with examples

Last Updated : 06 Mar, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

Stream of(T… values) returns a sequential ordered stream whose elements are the specified values. 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 cores of the computer.

Syntax :

static <T> Stream<T> of(T... values)

Where, Stream is an interface and T
is the type of stream elements.
values represents the elements of the 
new stream and the function 
returns the new stream.

Example 1 : Stream of strings.




// Java code for Stream.of()
// to get sequential ordered stream
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // creating a stream of Strings
        // and printing sequential
        // ordered stream
        Stream<String> stream = Stream.of("Geeks", "for", "Geeks");
  
        stream.forEach(System.out::println);
    }
}


Output :

Geeks
for
Geeks

Example 2 : Stream of Integers.




// Java code for Stream.of()
// to get sequential ordered stream
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // creating a stream of Integer
        // and printing sequential
        // ordered stream
        Stream<Integer> stream = Stream.of(5, 7, 9, 12);
  
        stream.forEach(System.out::println);
    }
}


Output :

5
7
9
12

Example 3 : Stream of Long primitives.




// Java code for Stream.of()
// to get sequential ordered stream
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // creating a stream of Long
        // and printing sequential
        // ordered stream
        Stream<Long> stream = Stream.of(4L, 8L, 12L, 16L, 20L);
  
        stream.forEach(System.out::println);
    }
}


Output :

4
8
12
16
20


Previous Article
Next Article

Similar Reads

Difference between Stream.of() and Arrays.stream() method in Java
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 Code // Java program to demonstrate Arrays.stream() method import java.uti
5 min read
Character Stream Vs Byte Stream in Java
A stream is a sequence of data. I/O Stream refers to a stream that is unlikely a method to sequentially access a file. I/O Stream means an input source or output destination representing different types of sources e.g. disk files. The java.io package provides classes that allow you to convert between Unicode character streams and byte streams of no
4 min read
Java Program to Convert String to Char Stream Without Using Stream
Char stream defines the array of characters. In this article, we will learn the different types of methods for converting a String into a char stream in Java without using Stream. Let us see some methods one by one. ExamplesInput: String = HelloGeeksOutput: [H, e, l, l, o, G, e, e, k, s] Input: String = Introduction Output: [I, n, t, r, o, d, u, c,
4 min read
foreach() loop vs Stream foreach() vs Parallel Stream foreach()
foreach() loopLambda operator is not used: foreach loop in Java doesn't use any lambda operations and thus operations can be applied on any value outside of the list that we are using for iteration in the foreach loop. The foreach loop is concerned over iterating the collection or array by storing each element of the list on a local variable and do
4 min read
Stream skip() method in Java with examples
Prerequisite : Streams in java The skip(long N) is a method of java.util.stream.Stream object. This method takes one long (N) as an argument and returns a stream after removing first N elements. skip() can be quite expensive on ordered parallel pipelines, if the value of N is large, because skip(N) is constrained to skip the first N elements in the
3 min read
Stream.reduce() in Java with examples
Many times, we need to perform operations where a stream reduces to single resultant value, for example, maximum, minimum, sum, product, etc. Reducing is the repeated process of combining all elements. reduce operation applies a binary operator to each element in the stream where the first argument to the operator is the return value of the previou
3 min read
Stream.max() method in Java with Examples
Stream.max() returns the maximum element of the stream based on the provided Comparator. A Comparator is a comparison function, which imposes a total ordering on some collection of objects. max() is a terminal operation which combines stream elements and returns a summary result. So, max() is a special case of reduction. The method returns Optional
3 min read
Stream min() method in Java with Examples
Stream.min() returns the minimum element of the stream based on the provided Comparator. A Comparator is a comparison function, which imposes a total ordering on some collection of objects. min() is a terminal operation which combines stream elements and returns a summary result. So, min() is a special case of reduction. The method returns Optional
2 min read
Stream generate() method in Java with examples
Stream generate(Supplier&lt;T&gt; s) returns an infinite sequential unordered stream where each element is generated by the provided Supplier. This is suitable for generating constant streams, streams of random elements, etc. Syntax : static &lt;T&gt; Stream&lt;T&gt; generate(Supplier&lt;T&gt; s) Where, Stream is an interface and T is the type of s
1 min read
Stream count() method in Java with examples
long count() returns the count of elements in the stream. This is a special case of a reduction (A reduction operation takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation). This is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect.
2 min read
Practice Tags :