Finite or Infinite Stream – Streams

Finite or Infinite Stream

The size of a stream can be finite or infinite depending on how the stream is created. The generate() and iterate() methods of the core stream interfaces create streams with an infinite number of elements (p. 894). Such a stream is said to be unbounded. The overloaded ints(), longs(), and doubles() methods of the java.util.Random class create streams with an effectively unlimited number of pseudorandom values (p. 900). An infinite stream must be truncated before the terminal operation is initiated; otherwise, the stream pipeline will never terminate (p. 917).

Object or Numeric Stream

The interface Stream<T> defines the contract for streams of object references—that is, object streams. The specialized interfaces IntStream, LongStream, and DoubleStream represent streams of int, long, and double values, respectively—that is, numeric streams. The various ways to create streams discussed here will always result in a stream whose element type is either a reference type or a numeric type (int, long, or double). Conversion between these stream types is discussed in §16.5, p. 934.

Table 16.1, p. 904, summarizes selected methods for building streams from various data sources.

The following static factory methods for building streams are defined in the Stream<T> class. Counterparts to these methods are also provided by the IntStream, LongStream, and DoubleStream interfaces for creating numeric streams, unless otherwise noted:

Click here to view code image

static <T> Stream<T> empty()
static <T> Stream<T> of(T t)
static <T> Stream<T> of(T… varargs)

The first method creates an empty stream—that is, a stream that has no elements.

The second method creates a singleton stream—that is, a stream that has a single element.

The third method creates a finite sequential ordered stream whose elements are the values specified by the variable arity parameter varargs.

The second and last methods throw a NullPointerException if the argument is null.

Click here to view code image

static <T> Stream<T> ofNullable(T t)       
Only in the
 Stream<T> interface.

Creates a singleton stream that has a single element, if the argument t is non-null; otherwise, it returns an empty stream.

Click here to view code image

static <T> Stream<T> generate(Supplier<T> supplier)

Creates an infinite sequential unordered stream where each element is generated by the specified supplier. Typically, this is used for constant streams and streams with random elements.

Click here to view code image

static <T> Stream<T> iterate(T s, UnaryOperator<T> uop)

Creates an infinite sequential ordered stream produced by the iterative application of the function uop to an initial element seed s, producing a stream consisting of s, e1=uop.apply(s), e2=uop.apply(e1), and so on; that is, uop is applied to the previous element.

Click here to view code image

static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b)

Creates a stream whose elements are all elements of the first stream followed by all elements of the second stream.

The resulting stream is ordered only if both input streams are ordered. If either input stream is parallel, the resulting stream is parallel. As one would expect, the resulting stream is finite only if both input streams are finite.

Leave a Reply

Your email address will not be published. Required fields are marked *