Building Streams – Streams
16.4 Building Streams A stream must have a data source. In this section we will explore how streams can be created from various data sources: collections, arrays, specified values, generator functions, strings, and I/O channels, among others. Aspects to Consider When Creating Streams When creating a stream from a data source, certain aspects to consider […]
Mapping a Numeric Stream to an Object Stream – Streams
Mapping a Numeric Stream to an Object Stream The mapToObj() method defined by the numeric stream interfaces transforms a numeric stream to an object stream of type R, and the boxed() method transforms a numeric stream to an object stream of its wrapper class. The query below prints the squares of numbers in a given […]
Execution Mode of a Stream – Streams
Execution Mode of a Stream The two methods parallel() and sequential() are intermediate operations that can be used to set the execution mode of a stream—that is, whether it will execute sequentially or in parallel. Only the Collection.parallelStream() method creates a parallel stream from a collection, so the default mode of execution for most streams […]
Filtering – Streams
Filtering Filters are stream operations that select elements based on some criteria, usually specified as a predicate. This section discusses different ways of filtering elements, selecting unique elements, skipping elements at the head of a stream, and truncating a stream. The following methods are defined in the Stream<T> interface, and analogous methods are also defined […]
Using Generator Functions to Build Infinite Streams – Streams
Using Generator Functions to Build Infinite Streams The generate() and iterate() methods of the core stream interfaces can be used to create infinite sequential streams that are unordered or ordered, respectively. Infinite streams need to be truncated explicitly in order for the terminal operation to complete execution, or the operation will not terminate. Some stateful […]
Lazy Execution – Streams
Lazy Execution A stream pipeline does not execute until a terminal operation is invoked. In other words, its intermediate operations do not start processing until their results are needed by the terminal operation. Intermediate operations are thus lazy, in contrast to the terminal operation, which is eager and executes when it is invoked. An intermediate […]
Streams from Arrays – Streams
Streams from Arrays We have seen examples of creating streams from arrays when discussing the variable arity of() method of the stream interfaces and the overloaded Arrays.stream() methods earlier in the chapter (p. 893). The sequential stream created from an array has the same order as the positional order of the elements in the array. […]
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. Click here to view code image Stream<CD> cdStream = Stream.empty(); // Empty stream of CD.System.out.println(“Count: ” + cdStream.count()); // Count: 0IntStream iStream = IntStream.empty(); // Empty […]
Numeric Streams Using the Random Class – Streams
Numeric Streams Using the Random Class The following methods for building numeric unordered streams are defined in the java.util.Random class: NumType is Int, Long, or Double, and the corresponding numtype is int, long, or double. The corresponding overloaded numtypes() methods are ints(), longs(), and doubles(). Click here to view code image NumTypeStreamnumtypes()NumTypeStreamnumtypes(numtype randomNumberOrigin, numtype randomNumberBound)NumTypeStreamnumtypes(long streamSize)NumTypeStreamnumtypes(long streamSize,numtype randomNumberOrigin, numtype randomNumberBound) The […]
Mapping: Transforming Streams – Streams
Mapping: Transforming Streams The map() operation has already been used in several examples (Example 16.3, p. 906, Example 16.4, p. 909, and Example 16.6, p. 920). Here we take a closer look at this essential intermediate operation for data processing using a stream. It maps one type of stream (Stream<T>) into another type of stream […]
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 |