Creating streams
Streams, both finite and infinite, can be generated from various sources. For example, sources such as arrays, collections, varargs, and files can be used. Let’s examine these in turn. For the moment, we will deal with non-primitive types; all the streams will be serial (non-parallel). Both primitive and parallel streams will be discussed in Chapter 16.
Streaming from an array
We will use Stream<T> Arrays.stream(T[] array) for this. This static method accepts an array of type T and returns Stream<T>. Figure 15.4 (CreatingStreams.java) presents an example:
Figure 15.4 – Streaming an array
In this figure, we declare a Double array (note that this is not a primitive double array). The stream object is created using the Arrays.stream(T[] array) method call. We start the stream off using the terminal operation count(). Lastly, we output the number of elements in the array. Note that this is just an example and...