Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

OCP: Doubt on re-using of stream

 
Vicky Roy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • 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.
 
Paweł Baczyński
Bartender
Posts: 1959
38
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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: 63
  • Mark post as helpful
  • send pies
  • 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: 1959
38
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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: 63
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Marshal
Posts: 52663
121
  • Mark post as helpful
  • send pies
  • 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?
     
    Jeanne Boyarsky
    author & internet detective
    Sheriff
    Posts: 35762
    412
    Eclipse IDE Java VI Editor
    • Mark post as helpful
    • send pies
    • 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: 52663
    121
    • Mark post as helpful
    • send pies
    • 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.
     
    Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
    the new thread boost feature: great for the advertiser and smooth for the coderanch user
    https://coderanch.com/t/674455/Thread-Boost-feature
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!