How it works...

Let's see in detail all the methods we used in this example to create streams:

  • First, we used the parallelStream() method of the List class. In fact, this method is defined in the Collection interface, so all the classes that implement this interface, such as the ArrayList, LinkedList, or TreeSet classes implement that method. You can use the stream() method to create a sequential stream or the parallelStream() method to create a parallel one.
  • Then, we used an implementation of the Supplier interface: the MySupplier class. The interface provides the get() method. This method is called each time the stream needs an element to process. You can create a stream with an infinite number of elements, so you should use a method that ...

Get Java 9 Concurrency Cookbook - Second Edition 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.