About Question enthuware.ocpjp.ii.v11.2.1853 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
sirtomash
Posts: 4
Joined: Mon Oct 17, 2016 8:45 am
Contact:

About Question enthuware.ocpjp.ii.v11.2.1853 :

Post by sirtomash »

Hi,

Is ordered guaranteed for foreach operation? According to doc:
void forEach(Consumer<? super T> action)
Performs an action for each element of this stream.
This is a terminal operation.

The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation does not guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism. For any given element, the action may be performed at whatever time and in whatever thread the library chooses. If the action accesses shared state, it is responsible for providing the required synchronization.

Parameters:
So I understand that line //4 shall also be replaced with forEachOrdered ?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.1853 :

Post by admin »

The JavaDoc API description implies that it is non-deterministic for parallel streams. If you have a sequential stream, as in the code given in the question, then forEach will process in order.

Since, the API for Stream is used for all kinds of streams, you cannot know whether a stream reference that you have received points to sequential or a parallel stream. That is why the JavaDoc says that it is non-deterministic.

However, in this case, we are creating a sequential stream and so we know that forEach will process elements in order.
If you like our products and services, please help us by posting your review here.

steinov
Posts: 19
Joined: Wed Feb 08, 2023 3:11 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.1853 :

Post by steinov »

Hi,

The doc mentioned in the answer has an example where a parallelstream is used with forEachOrdered:

Code: Select all

Integer[] intArray = {1, 2, 3, 4, 5, 6, 7, 8 };
List<Integer> listOfIntegers =
    new ArrayList<>(Arrays.asList(intArray));

System.out.println("With forEachOrdered:");
listOfIntegers
    .parallelStream()
    .forEachOrdered(e -> System.out.print(e + " "));
System.out.println("");

// output:
// With forEachOrdered:
// 8 7 6 5 4 3 2 1

The [above pipeline] uses the method forEachOrdered, which processes the elements of the stream in the order specified by its source, regardless of whether you executed the stream in serial or parallel. Note that you may lose the benefits of parallelism if you use operations like forEachOrdered with parallel streams.
The above output seems to contradict the statement in the doc about the pipeline and the answer to this question. I would expect the output to be ordered, but it is not. Am I missing something?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.1853 :

Post by admin »

The code in that example sorts the list in reverse order using Collections.sort(listOfIntegers, reversed);
If you like our products and services, please help us by posting your review here.

steinov
Posts: 19
Joined: Wed Feb 08, 2023 3:11 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.1853 :

Post by steinov »

Oh wow, don't know how I didn't see that. Thanks.

Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests