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
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Hashmap methods in Java with Examples | Set 2 (keySet(), values(), containsKey()..)
- Map Values() Method in Java With Examples
- ChronoUnit values() method in Java with Examples
- Provider values() method in Java with Examples
- TimeUnit values() method in Java with Examples
- AbstractMap values() Method in Java with Examples
- ConcurrentHashMap values() method in Java with Examples
- SortedMap values() method in Java with Examples
- DayOfWeek values() method in Java with Examples
- Locale.Category values() method in Java with Examples
- Locale.FilteringMode values() method in Java with Examples
- ChronoField values() method in Java with Examples
- TextStyle values() method in Java with Examples
- FormatStyle values() method in Java with Examples
- SignStyle values() method in Java with Examples
- ResolverStyle values() method in Java with Examples
- Assigning values to static final variables in Java
- Returning Multiple values in Java
- Default array values in Java
- Assigning long values carefully in Java to avoid overflow
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

