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 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.
import java.util.stream.Stream;
class GFG {
public static void main(String[] args)
{
Stream<String> stream = Stream.of( "Geeks" , "for" , "Geeks" );
stream.forEach(System.out::println);
}
}
|
Output :
Geeks
for
Geeks
Example 2 : Stream of Integers.
import java.util.stream.Stream;
class GFG {
public static void main(String[] args)
{
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.
import java.util.stream.Stream;
class GFG {
public static void main(String[] args)
{
Stream<Long> stream = Stream.of(4L, 8L, 12L, 16L, 20L);
stream.forEach(System.out::println);
}
}
|
Output :
4
8
12
16
20
Last Updated :
06 Mar, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...