Stream.of(T t) in Java with examples
Last Updated :
06 Dec, 2018
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 cores of the computer.
Syntax :
static <T> Stream<T> of(T t)
Where, Stream is an interface and T
is the type of stream elements.
t is the single element and the
function returns a singleton
sequential stream.
Example 1 : Singleton string stream.
import java.util.stream.Stream;
class GFG {
public static void main(String[] args)
{
Stream<String> stream = Stream.of( "Geeks" );
stream.forEach(System.out::println);
}
}
|
Output :
Geeks
Example 2 : Singleton Integer Stream.
import java.util.stream.Stream;
class GFG {
public static void main(String[] args)
{
Stream<Integer> stream = Stream.of( 5 );
stream.forEach(System.out::println);
}
}
|
Output :
5
Example 3 : Singleton Long stream.
import java.util.stream.Stream;
class GFG {
public static void main(String[] args)
{
Stream<Long> stream = Stream.of(4L);
stream.forEach(System.out::println);
}
}
|
Output :
4
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...