New methods have been added to Java libraries to return a stream.
We can create stream in the following ways.
In the following sections we will learn how to create streams.
We can use of()
from Stream interface
to create a sequential Stream from a single value and multiple values .
<T> Stream<T> of(T t) <T> Stream<T> of(T...values)
From the method signature we can see that the first of() method creates a stream from a single value while the second of() method creates a stream from varied length parameters
The following code creates a stream which contains a single value.
import java.util.stream.Stream; public class Main { public static void main(String[] args) { Stream<String> stream = Stream.of("java2s.com"); stream.forEach(System.out::println); } }
The code above generates the following result.
The following code creates a stream with four strings.
import java.util.stream.Stream; public class Main { public static void main(String[] args) { Stream<String> stream = Stream.of("XML", "Java", "CSS", "SQL"); stream.forEach(System.out::println); } }
The code above generates the following result.
The following code creates a stream from an array of objects.
import java.util.stream.Stream; public class Main { public static void main(String[] args) { String[] names = { "XML", "Java", "SQL", "CSS" }; Stream<String> stream = Stream.of(names); stream.forEach(System.out::println); } }
The code above generates the following result.
We can use Stream.Builder<T>
to create streams.
The following code creates a stream builder.
Stream.Builder<String> builder = Stream.builder();
import java.util.stream.Stream; //from w ww. j a v a 2 s.c om public class Main { public static void main(String[] args) { Stream<String> stream = Stream.<String>builder() .add("XML") .add("Java") .add("CSS") .add("SQL") .build(); stream.forEach(System.out::println); } }
The code above generates the following result.
We can use the following two methods from IntStream interfaces to create IntStream from a range of int values.
IntStream range(int start, int end) IntStream rangeClosed(int start, int end).
They create an IntStream that contains ordered integers between the start and end.
The specified end is exclusive in the range() method and inclusive in the rangeClosed() method.
The following code uses both methods to create an IntStream having integers 1, 2, 3, 4, and 5 as their elements:
import java.util.stream.IntStream; public class Main { public static void main(String[] args) { IntStream oneToFive = IntStream.range(1, 6); //IntStream oneToFive = IntStream.rangeClosed(1, 5); oneToFive.forEach(System.out::println); } }
Like the IntStream interface, the LongStream class also contains range() and rangeClosed() methods that takes arguments of type long and return a LongStream.
The code above generates the following result.
An empty stream has no elements.
We can use empty() static method from Stream interface to create an empty sequential stream.
import java.util.stream.Stream; public class Main { public static void main(String[] args) { Stream<String> stream = Stream.empty(); stream.forEach(System.out::println); } }
The IntStream, LongStream, and DoubleStream interfaces also contain an empty() static method to create an empty stream of primitive types.
The following code creates an empty stream of integers.
IntStream numbers = IntStream.empty();