Taking a Leap to Parallelize

We can easily parallelize the previous example to speed up the execution.

Let’s take a closer look at the code before we make any changes. We send the tickers though the map method, which sequentially invokes the attached function to get the price from Yahoo! Once all the prices arrive, we sequentially trim the list to stocks within the desired price range. In the last step, which is also sequential, we pick the highest priced among them.

Of these three operations, the first is the slowest. It involves a call to the web service, incurs a network delay, and has to perform the operation 20 times—once for each ticker symbol. The remaining operations involve light arithmetic and hardly take any time. Thankfully, in this ...

Get Functional Programming in Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.