Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Errata OCP Appendix A Chapter 7 Question 8.  RSS feed

 
Jerzy Los
Greenhorn
Posts: 6
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a doubt about a one sentence in an explanation.

The findAny() method will return the value from the first thread that retrieves a record. Therefore, the output is not guaranteed for either serial or parallel stream


I think findAny() on serial Stream will output first element as described in Chapter 7.

System.out.print(Arrays.asList(1,2,3,4,5,6).stream().findAny().get());
This code consistently outputs the first value in the serial stream, 1.


 
Roel De Nijs
Marshal
Posts: 10996
161
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the javadoc of the findAny() method, the behavior of this operation is explicitly nondeterministic; it is free to select any element in the stream.

So in a non-parallel operation, it will most likely return the first element in the Stream but there is no guarantee for this.
 
Jerzy Los
Greenhorn
Posts: 6
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I see that there was already a topic about that, but it wasn't added to the errata page.

https://coderanch.com/t/677243/certification/findAny-Enthuware-Boyarsky-Selikoff

Regards,
jml
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36437
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerzy Los wrote:I see that there was already a topic about that, but it wasn't added to the errata page.

https://coderanch.com/t/677243/certification/findAny-Enthuware-Boyarsky-Selikoff

That's because I don't believe it is an errata. While I agree that a serial stream does return the first element on findAny(), I don't think it is guaranteed by the specification.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!