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:
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.
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.
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.
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.
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.
Archives
- July 2024
- June 2024
- May 2024
- March 2024
- February 2024
- January 2024
- December 2023
- October 2023
- September 2023
- May 2023
- March 2023
- January 2023
- December 2022
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- April 2022
- March 2022
- November 2021
- October 2021
- September 2021
- July 2021
- June 2021
- March 2021
- February 2021
Calendar
M | T | W | T | F | S | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |