We just exploited theparallelStream()method to return a sequential stream, even though the name suggestsparallelStream. This is where the method differs fromstream().parallel(), which always tries to return a p
Let’s look at an example where we first callforEach()directly on the collection, and second, on a parallel stream: list.forEach(System.out::print); System.out.print(" "); list.parallelStream().forEach(System.out::print); If we run the code several times, we see thatlist.forEach(...
And collect() works as follows. public class StreamCollect { public static void main(String[] args) { List<String> list = Arrays.asList("Mukesh", "Vishal", "Amar"); String result = list.parallelStream().collect(StringBuilder::new, ...