181. Infinite streams, takeWhile(), and dropWhile()

In the first part of this problem, we will talk about infinite streams. In the second part, we will talk about the takeWhile() and dropWhile() APIs.

An infinite stream is a stream that creates data indefinitely. Because streams are lazy, they can be infinite. More precisely, creating an infinite stream is accomplished as an intermediate operation, and so no data is created until a terminal operation of the pipeline is executed.

For example, the following code will theoretically run forever. This behavior is triggered by the forEach() terminal operation and caused by a missing constraint or limitation:

Stream.iterate(1, i -> i + 1)
.forEach(System.out::println);

The Java Stream API allows us to create and manipulate an infinite stream in several ways, as you will see shortly.

Moreover, a Stream can be ordered or unordered, depending on the defined encounter order. Whether or not a stream has an encounter order depends on the source of data and the intermediate operations. For example, a Stream that has a List as its source is ordered because List has an intrinsic ordering. On the other hand, a Stream that has a Set as its source is unordered because Set doesn't guarantee order. Some intermediate operations (for example, sorted()) may impose an order to an unordered Stream, while some terminal operations (for example, forEach()) may ignore the encounter order.

Commonly, the performance of sequential streams is insignificantly affected by ordering, but depending on the applied operations, the performance of parallel streams may be significantly affected by the presence of an ordered Stream.

Don't confuse Collection.stream().forEach() with Collection.forEach(). While Collection.forEach() can keep order by relying on the collection's iterator (if any), the Collection.stream().forEach() order undefined. For example, iterating a List several times via list.forEach() processes the elements in insertion order, while list.parallelStream().forEach() produces a different result at each run. As a rule of thumb, if a stream is not needed, then iterate over a collection via Collection.forEach().

We can turn an ordered stream into an unordered stream via BaseStream.unordered(), as shown in the following example:

List<Integer> list 
= Arrays.asList(1, 4, 20, 15, 2, 17, 5, 22, 31, 16);

Stream<Integer> unorderedStream = list.stream()
.unordered();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.133.49