Parallel Operations
With the addition of Lambda expressions to arrays operations, Java introduced a key concept into the language of internal iteration. Essentially as developers we’re used to use loop operations as one of the most basic programming idioms, right up there with if and else.
The introduction of Lambda expressions turned that paradigm around with the actual iteration over a collection over which a Lambda function is invoked is carried out by the core library.
You can think of this as an extension of iterators where the actual operation of extracting the next item from a collection on which to operate is carried out by an iterator. An exciting possibility opened by this design pattern is to enable operations carried out on long arrays such as sorting, filtering and mapping to be carried out in parallel by the framework. When dealing with server code that’s processing lengthy collections on a continuous basis, this can lead to major throughput improvements with relative work from your end.
Here’s the same snippet as above, but using the framework’s new parallel processing capabilities -
ConcurrentMap<Person.Sex, List<Person>> byGender =
roster.parallelStream().collect(
Collectors.groupingByConcurrent(Person::getGender))