![]() In such cases, we need to use a function to combine the results of the substreams into a single one. When a stream executes in parallel, the Java runtime splits the stream into multiple substreams. Int computedAges = ages.parallelStream().reduce(0, (a, b) -> a + b, Integer::sum) In addition, we can use reduce() in a parallelized stream (more on this later): List ages = Arrays.asList(25, 30, 45, 28, 32) "", (partialString, element) -> partialString.toUpperCase() + element.toUpperCase()) Let’s use the reduce() operation for joining the uppercase elements of the letters array: String result = letters Similarly, we can switch to the version that uses a method reference: String result = letters.stream().reduce("", String::concat) reduce("", (partialString, element) -> partialString + element) For instance, we can use reduce() on an array of String elements and join them into a single result: List letters = Arrays.asList("a", "b", "c", "d", "e")
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |