What is Difference Between parallelStream() and stream().parallel() in Java

In Java 8, the Stream interface provides two methods for creating parallel streams: parallelStream() and stream().parallel(). Both of these methods can be used to create parallel streams, but there are some subtle differences between them:

parallelStream() : This method is provided directly by the Stream interface, and it returns a parallel stream. This method is easy to use, and it is the preferred way of creating parallel streams.

stream().parallel(): This method is provided by the Stream interface as well, but it returns a stream that is transformed into a parallel stream. This method is useful when you already have a stream object and want to switch it to a parallel stream.

The main difference between these two methods is in how they affect the order of the stream's elements. When you use parallelStream(), the order of the elements in the resulting parallel stream may not be the same as the order of the elements in the original stream. This is because the elements are processed in parallel, and their order of completion may be different.

On the other hand, when you use stream().parallel(), the order of the elements in the resulting parallel stream will be the same as the order of the elements in the original stream. This is because the stream is first created in the same order as the original stream, and then it is transformed into a parallel stream.

Here is an example to illustrate the difference between parallelStream() and stream().parallel():

package com.javacodestuffs.core.java8; import java.util.Arrays; import java.util.List; public class Test2 { public static void main(String[] args) { // TODO Auto-generated method stub List<String> list = Arrays.asList("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"); System.out.println("--------------- Using parallelStream() -----------------"); list.parallelStream().forEach(System.out::println); // May print Friday /* Thursday Sunday Saturday Tuesday Wednesday Monday or some other order */ System.out.println("------------- sing stream().parallel()-------------------"); list.stream().parallel().forEach(System.out::println); /* May print Friday Tuesday Thursday Wednesday Saturday Sunday Monday */ } }

In this example, we have a list of Strings, and we are using both parallelStream() and stream().parallel() to create parallel streams. When we use parallelStream(), the order of the elements in the resulting stream may not be the same as the original order. However, when we use stream().parallel(), the order of the elements in the resulting stream is the same as the original order.

Overall, both parallelStream() and stream().parallel() can be used to create parallel streams in Java, but the choice between them depends on whether or not the order of the elements in the original stream needs to be preserved in the parallel stream.

In this article, we have seen the Singleton Class design pattern in Java using different ways. All source code in the article can be found in the GitHub repository.