[HD-OCP17/21-Fundamentals Pg 779, Sec. 21.3.4 - quiz]
Moderator: admin
-
- Site Admin
- Posts: 10383
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
[HD-OCP17/21-Fundamentals Pg 779, Sec. 21.3.4 - quiz]
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."
-
- Posts: 21
- Joined: Sat Sep 01, 2018 10:53 am
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 779, Sec. 21.3.4 - quiz]
E should not be considered correct — we have a sequential stream, and findFirst() short-circuits, processing only the first element.
-
- Site Admin
- Posts: 10383
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 779, Sec. 21.3.4 - quiz]
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.
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.
-
- Posts: 21
- Joined: Sat Sep 01, 2018 10:53 am
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 779, Sec. 21.3.4 - quiz]
However, only Mapping 1 will be printed, as elements 2, 3, and 4 are never mapped in a sequential stream with findFirst()
-
- Site Admin
- Posts: 10383
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 779, Sec. 21.3.4 - quiz]
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.

Please tell me your name and you will be credited in the acknowledgements page of the book.
-
- Posts: 21
- Joined: Sat Sep 01, 2018 10:53 am
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 779, Sec. 21.3.4 - quiz]
Thanks for the kind offer, but my name was already added to the acknowledgements page two months ago 

Who is online
Users browsing this forum: Bing [Bot] and 13 guests