Parallel Streams in Java with code samples
In my earlier article, I had covered the Stream API in depth. In this article, we will be taking a look at parallel streams. Simply speaking, a parallel stream is a stream that operates on its elements in parallel. How to obtain a parallel stream Just like the stream method, all the Collection interfaces have a parallelStream method. This returns a parallel stream on the underlying Collection. Consider the following code: List<String> food = Arrays.asList("Pizza","Sandwich","Burger","Rice");Stream<String> foodStr = food.parallelStream(); This code first creates a String List food with some values. It then invokes the parallelStream method on food. This returns a String Stream which is a parallel stream. In addition, the Stream interface also has a method parallel . If the stream on which this method is invoked is a sequential stream, it returns a parallel stream corresponding to it. If the stream on which it is invoked in a paral...