Exam 1z0-829 All QuestionsBrowse all questions from this exam
Question 13

Given the code fragment:

What is the result?

    Correct Answer: A

    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.

Discussion
james2033Option: C

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