From the course: Advanced Java Programming

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Implementing parallel streams

Implementing parallel streams - Java Tutorial

From the course: Advanced Java Programming

Start my 1-month free trial

Implementing parallel streams

- [Instructor] One of the advantages of using streams over using for loops, is that you can run iterations in parallel. In this example, I'm using my code which simulates a library of books. This creates a list of book objects. In the main method I use a stream, and I filter it to see if the author of the book begins with J. At the end I print out these books. So if I run the program now, it prints out the six books which have an author beginning with J. Now I want to make use of parallel streams so that multiple cores can be used at the same time to filter the elements. This sounds like a complicated taste to implement and if I was using for loops, it would be very complicated, however, with streams, it is extremely easy. The only thing I need to change in my code is to change the name of the stream method to the parallel stream method. All the implementation is done for me under the hood and I now have parallel streams. If I run the program again, I get the same output. The…

Contents