Given the code fragment:
What is the result?
Given the code fragment:
What is the result?
When you concatenate two streams, make them parallel, apply distinct, and then forEach, the output is unordered because forEach does not guarantee the order of elements when working on parallel streams. Therefore, the elements 'A', 'B', 'C', 'D', and 'E' will be printed in an unpredictable order, but all duplicates will be removed, leaving 'A', 'B', 'C', 'D', and 'E' each printed exactly once.
package q13; import java.util.stream.Stream; public class Q13 { public static void main(String[] args) { Stream<String> s1 = Stream.of("A", "B", "C", "B"); Stream<String> s2 = Stream.of("A", "D", "E"); Stream.concat(s1, s2).parallel().distinct().forEach(element -> System.out.print(element)); } } // Result: // CEDBA // CEDAB // CEDBA