Java 17 Recipes
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
- Bu sahifa navigatsiya:
- 2-15. Utilizing Factory Methods to Create Immutable Collections Problem
How It Works
Streams changed how we develop code and handle data collections in Java. The original set of filters available for use with streams was fairly generous. However, from Java 8, more options have been added, making it even easier to refine data with streams. The takeWhile() and dropWhile() constructs allow streams to be parsed. On one side they return a new stream that contains all elements before the first one that fails the specified predicate condition. On the other side they return a new stream containing all elements including and after the first element that fails a specified predicate, respectively. The solution to this recipe parses the list of strings and prints each element to the terminal window for the first pass. The takeWhile() construct is then applied to the same stream of strings, and the elements from the stream before the element that fails the specified condition are printed to the terminal window. The takeWhile() accepts a predicate condition, which it then applies to each element in the stream. Then only those elements that are iterated before the predicate condition that is not matched is returned. All elements that reside in the stream at and after the position where the condition is not met are not returned. The opposite result occurs when using the dropWhile() construct. In the solution, all stream elements are ignored until the first element upon which the specified condition that is no longer met is returned. Each subsequent element in the stream is also returned. The takeWhile and dropWhile constructs are very similar to the filter, except that only one failed condition causes the remaining elements to be ignored or returned, respectively. 2-15. Utilizing Factory Methods to Create Immutable Collections Problem You wish to generate an immutable collection (its state does not change after it is constructed) of values. Chapter 2 enhanCements from Java 9 through Java 17 |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling