This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

OCP: Doubt on re-using of stream  RSS feed

 
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: 2054
44
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: 2054
44
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: 55717
163
  • 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: 37241
    519
    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: 55717
    163
    • 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.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!