The Empty Stream – Streams
The Empty Stream
An empty stream can be obtained by calling the empty() method of the core stream interfaces. As the name implies, such a stream has no elements.
Stream<CD> cdStream = Stream.empty(); // Empty stream of CD.
System.out.println(“Count: ” + cdStream.count()); // Count: 0
IntStream iStream = IntStream.empty(); // Empty stream of int.
System.out.println(“Count: ” + iStream.count()); // Count: 0
The count() method is a terminal operation in the Stream<T> interface (p. 953). It returns the number of elements processed through the stream pipeline.
Using the null value to indicate that a stream is empty may result in a NullPointer-Exception. Therefore, using an explicit empty stream is highly recommended.
Streams from Specified Values
The two overloaded of() methods in the core stream interfaces create finite sequential ordered streams from data values that are specified as arguments.
In the code below, the single-argument of() method is called at (1), and the variable arity of() method is called at (2), both creating a stream of element type CD. The size of the streams created at (1) and (2) is 1 and 3, respectively. The stream pipeline comprising (3) and (4) filters the pop music CDs and prints their title at (4). The forEach() terminal operation at (4) applies its Consumer action to each pop music CD.
// From specified objects.
Stream<CD> cdStream1 = Stream.of(CD.cd0); // (1) Single-arg call.
Stream<CD> cdStream2 = Stream.of(CD.cd0, CD.cd1, CD.cd2); // (2) Varargs call.
cdStream2.filter(CD::isPop) // (3)
.forEach(cd -> System.out.println(cd.title())); // (4)
The code below shows examples of using numeric values to create streams. The values specified at (1) and (2) are autoboxed to create a stream of objects. The declaration statements at (3) and (4) avoid the overhead of autoboxing when streams of numeric values are created. However, at (4), an implicit numeric conversion to double is applied to the non-double values.
// From specified numeric values.
Stream<Integer> integerStream1 = Stream.of(2017, 2018, 2019); // (1)
Stream<? extends Number> numStream = Stream.of(100, 3.14D, 5050L); // (2)
IntStream intStream1 = IntStream.of(2017, 2018, 2019); // (3)
DoubleStream doubleStream = DoubleStream.of(100, 3.14D, 5050L); // (4)
The variable arity of() method can be used to create a stream whose source is an array. Equivalently, the overloaded Arrays.stream() method can be used for the same purpose. In all cases below, the size of the stream is the same as the size of the array, except at (7). An int array is an object that is passed to the single-argument Stream.Of() method (creating a Stream<int[]>), and not the variable arity Stream.of() method. The int array is, however, passed to the variable arity IntStream.of() method at (8). Creating a stream from a numeric array is safer with the numeric stream interfaces or the Arrays.stream() method than the Stream.of() method.
// From an array of CDs.
Stream<CD> cdStream3 = Stream.of(CD.cdArray); // (1)
Stream<CD> cdStream4 = Arrays.stream(CD.cdArray); // (2)
// From an array of Integer.
Integer[] integerArray = {2017, 2018, 2019}; // (3)
Stream<Integer> integerStream2 = Stream.of(integerArray); // (4)
Stream<Integer> integerStream3 = Arrays.stream(integerArray); // (5)
// From an array of int.
int[] intArray = {2017, 2018, 2019}; // (6)
Stream<int[]> intArrayStream = Stream.of(intArray); // (7) Size is 1.
IntStream intStream2 = IntStream.of(intArray); // (8) Size is 3.
IntStream intStream3 = Arrays.stream(intArray); // (9) Size is 3.
The Stream.of() methods throw a NullPointerException if the argument is null. The ofNullable() method, on the other hand, returns an empty stream if this is the case; otherwise, it returns a singleton stream.
Leave a Reply