• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

findAny() Enthuware vs Boyarsky/Selikoff  RSS feed

 
Ranch Hand
Posts: 39
5
Java Linux Spring
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have noticed an inconsistency, or difference of opinion regarding findAny() from the Enthuware exams and The Boyarsky/Selikoff OCP 8 book.

So in the book, Chap 7, p372

Since order is not guaranteed with parallel streams, methods such as findAny() on parallel streams may result in unexpected behavior. Let’s take a look at the results of findAny() applied to a serial stream:

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.

With a parallel stream, the JVM can create any number of threads to process the stream. When you call findAny() on a parallel stream, the JVM selects the first thread to finish the task and retrieves its data:

System.out.print(Arrays.asList(1,2,3,4,5,6).parallelStream().findAny().get());

The result is that the output could be 4, 1, or really any value in the stream. You can see that with parallel streams, the results of findAny() are no longer predictable.



So in the book, it details, or rather implies, that the output of findAny(), when used in a sequential/in series stream, is deterministic.

However, Whilst doing an Enthuware practice test, I came across the following question (Q30 - OCP upgrade 7 to 8 test 1):


What will the following code print?

Object v1 = IntStream.rangeClosed(10, 15)
       .boxed()
       .filter(x->x>12)
       .parallel()
       .findAny();

Object v2 = IntStream.rangeClosed(10, 15)
       .boxed()
       .filter(x->x>12)
       .sequential()
       .findAny();

System.out.println(v1+":"+v2);

(Note: < and > in the options below denote the possible output and not the sign themselves.)



With the Answer being:

<An Optional containing 13, 14, or 15>:<An Optional containing 13, 14, or 15>



With the explaination:

The second stream is sequential and therefore, ideally, findAny should return the first element. However, findAny is deliberately designed to be non-deterministic. Its API specifically says that it may return any element from the stream. If you want to select the first element, you should use findFirst.



Which is entirely true. In the API it states:

"The behavior of this operation is explicitly nondeterministic; it is free to select any element in the stream. This is to allow for maximal performance in parallel operations; the cost is that multiple invocations on the same source may not return the same result. (If a stable result is desired, use findFirst() instead.)



This has also been discussed on the Enthuware forum, here: http://enthuware.com/forum/viewtopic.php?f=2&t=4148

So we clearly have a difference of opinion. I've ran this on my machine and I always get the first element. But the API clearly states that I should not rely on that, and it does not explicitly state that it is nondeterministic for parallel streams exlusively.

Great, and in practice, I would use findFirst() if I wanted the first element regardless of stream. Problem solved... (unless you want to pass the exam)....

So, in an exam, if I were to get a similar question relating to findAny() on a sequential stream, do I:
  • Select the first element in the stream, as per the book and as per the current implementation of the JDK I am running.
  • Select the option that could indicate that it could select any element of the stream, because the API says it is non-deterministic.


  • Really, I understand both sides. I just want to be able to select the option that will give me the mark.
     
    Ranch Hand
    Posts: 99
    15
    Chrome Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think, Oracle's explanation (API) should give the mark
    But it's good to have such topics to pay attention on this. Thanks!
     
    author & internet detective
    Posts: 39283
    727
    Eclipse IDE Java VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm 99.99999% sure the exam won't ask you that. (have a few more 9's handy? I ran out)  But if it it does, you should go with the API where it isn't guaranteed.
     
    Enthuware Software Support
    Posts: 4308
    35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Jeanne Boyarsky wrote:I'm 99.99999% sure the exam won't ask you that. (have a few more 9's handy? I ran out)  But if it it does, you should go with the API where it isn't guaranteed.


    Why do you think they won't ask this?  findAny is explicitly listed in exam objectives. Don't you think what it returns falls squarely under what one needs to know about this method?

    -Paul.
     
    Jeanne Boyarsky
    author & internet detective
    Posts: 39283
    727
    Eclipse IDE Java VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Let me rephrase that. i don't think they will ask it like we did where we have output from findAny() printing the first. Because that is an implementation detail of the current implementation.
     
    James Pittendreigh
    Ranch Hand
    Posts: 39
    5
    Java Linux Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for all the replies - this helps a lot.

    I tweeted the question to OracleCertification, which resulted in radio silence. You guys are much better. Thanks again.

    The next thing I need to remember is that there's no overloaded version that takes a predicate.
     
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mikalai Zaikin Quiz says: "although findAny is explicity nondeterministic, no effort will be made to randomize the element returned, and processing will start from 1st element for sequential stream.".

    It's a very confusing topic indeed.  
     
    I am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
    how do I do my own kindle-like thing - without amazon
    https://coderanch.com/t/711421/engineering/kindle-amazon
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!