Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Stream of() method in Java

  • Last Updated : 08 Oct, 2018

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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Return Value: Stream of(T t) returns a sequential Stream containing the single specified element.

Example :






// Java code for Stream of(T t)
// to get a sequential Stream
// containing a single element.
  
import java.util.*;
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an Stream having single element only
        Stream stream = Stream.of("Geeks");
  
        // Displaying the Stream having single element
        stream.forEach(System.out::println);
    }
}
Output:
Geeks

Stream of(T… values)

Stream of(T… values) returns a sequential ordered stream whose elements are the specified values.

Syntax :

static Stream of(T... values)

Parameters: This method accepts a mandatory parameter values which are the elements of the new stream.

Return Value : Stream of(T… values) returns a sequential ordered stream whose elements are the specified values.

Example:




// 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(System.out::println);
    }
}
Output:
Geeks
for
Geeks



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!