Page 1 of 1
[HD-OCP17/21-Fundamentals Pg 779, Sec. 21.3.4 - quiz]
Posted: Fri Sep 27, 2024 6:11 am
by admin
The answer A B C E is correct and the explanation is also correct. But the line "Hence, A is correct and C is incorrect." should be changed to "Hence, A and C are correct."
Re: [HD-OCP17/21-Fundamentals Pg 779, Sec. 21.3.4 - quiz]
Posted: Sun Apr 20, 2025 8:07 am
by Veilok
E should not be considered correct — we have a sequential stream, and findFirst() short-circuits, processing only the first element.
Re: [HD-OCP17/21-Fundamentals Pg 779, Sec. 21.3.4 - quiz]
Posted: Sun Apr 20, 2025 9:51 am
by admin
1. As the explanation notes, the stream is ordered, so no matter whether you insert a call to parallel() or sequential(), findFirst() will always return the first element (because it is the terminal operation).
2. The explanation also says, "The execution order of intermediate operations is not guaranteed even for ordered streams when the stream is parallel. " and map() is an intermediate operation and the given stream pipeline includes a call to parallel(), therefore, the map() operation will be executed in parallel and so Mapping 1 to Mapping 4 can be printed in any order.
Short circuiting behavior cannot guarantee that only 1 element will be processed if the stream is parallel because multiple threads are processing elements in parallel.
But if you change it to sequential() (instead of parallel()) then Mapping 1 to Mapping 4 will be printed in that order, which is what option E says. Therefore, E is a correct option.
Re: [HD-OCP17/21-Fundamentals Pg 779, Sec. 21.3.4 - quiz]
Posted: Sun Apr 20, 2025 5:15 pm
by Veilok
However, only Mapping 1 will be printed, as elements 2, 3, and 4 are never mapped in a sequential stream with findFirst()
Re: [HD-OCP17/21-Fundamentals Pg 779, Sec. 21.3.4 - quiz]
Posted: Sun Apr 20, 2025 11:31 pm
by admin
Duh...you are absolutely correct. Overlooked that point
Please tell me your name and you will be credited in the acknowledgements page of the book.
Re: [HD-OCP17/21-Fundamentals Pg 779, Sec. 21.3.4 - quiz]
Posted: Sun Apr 20, 2025 11:50 pm
by Veilok
Thanks for the kind offer, but my name was already added to the acknowledgements page two months ago
