• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

OCP: Doubt on re-using of stream

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stream can't be used again but OCP book (author Jeanne Boyarsky) states one question wrong (Chapter 4, Question 3) while other one is correct (Chapter 4, Question 14).
Both questions are using same stream though.




Thanks.
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the second example there is no reusing of streams. Each stream is used only once (or would be used if the code compiled).

By "used" I refer to a terminal operation being invoked.
 
Vicky Roy
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:In the second example there is no reusing of streams. Each stream is used only once (or would be used if the code compiled).

By "used" I refer to a terminal operation being invoked.



In 2nd example line 5 and 6 is using same s variable means same stream. And as per description given for first example, once stream is used can't be re-used.
 
Paweł Baczyński
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vicky Roy wrote:In 2nd example line 5 and 6 is using same s variable means same stream. And as per description given for first example, once stream is used can't be re-used.



There is only one terminal operation invoked on s2 stream. This means the stream is not being re-used.
 
Vicky Roy
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:

Vicky Roy wrote:In 2nd example line 5 and 6 is using same s variable means same stream. And as per description given for first example, once stream is used can't be re-used.



There is only one terminal operation invoked on s2 stream. This means the stream is not being re-used.



That seems to be new concept for me "if terminal operation is not involved then stream can be reused". I don't think it was discussed in the book.

Anyways, it means "nonMatch" and "anyMatch" is terminal operations and "mapToInt" not? Right?
 
Marshal
Posts: 80775
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It isn't nonMatch, but noneMatch. The xxxMatch methods are equivalent to quantifications:
  • allMatch ≡ ∀
  • anyMatch ≡ ∃
  • noneMatch ≡ ∀•¬ ≡ ¬∃
  • Have you read the documentation for Stream? It explains there what the difference is between terminal and internediate operations on Streams, and each method has a comment saying something like, “this is a stateful intermediate operation.”

    What did you think the correct answers to the questions you quoted were, and what were the answers shown in the book?
     
    author & internet detective
    Posts: 42154
    937
    Eclipse IDE VI Editor Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:It isn't nonMatch, but noneMatch. The xxxMatch methods are equivalent to quantifications:

  • allMatch ≡ ∀
  • anyMatch ≡ ∃
  • noneMatch ≡ ∀•¬ ≡ ¬∃
  • Have you read the documentation for Stream? It explains there what the difference is between terminal and internediate operations on Streams, and each method has a comment saying something like, “this is a stateful intermediate operation.”

    What did you think the correct answers to the questions you quoted were, and what were the answers shown in the book?


    #3 - E
    #14 - D

    $14 doesn't compile (hence D). So it isn't in conflict with #3 in any way!
     
    Campbell Ritchie
    Marshal
    Posts: 80775
    489
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I tried it and the first one threw an exception because the Stream had been used and cannot be rewound to its beginning. I had guessed it would throw an exception, but got the wrong exception.
    The second one is easier; you have a conversion of a double to an int without an explicit cast, and I thought the compiler wouldn't be happy with that.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic