• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Sybex CSG 17 Chapter 10 Review questions possible error

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there might be an issue with practice question 4 in Chapter 10

Practice question 4 in Chapter 10 states:
Which are true statements about terminal operations in a stream that runs successfully?
A. At most one terminal operation can exist in a stream pipeline.
B. Terminal operations are a required part of the stream pipeline in order to get a result.
(rest omitted)

The answer states
4. A, B. Terminal operations are the final step in a stream pipeline. Exactly one is required, because it triggers the execution of the entire stream pipeline. Therefore options A and B are correct. (rest omitted)

If exactly one terminal operation is required, wouldn't A be false because 0 is also at most 1?
 
Bartender
Posts: 5584
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Ruben,

welcome to the Ranch and enjoy the stay!

A stream can be without a terminal operation. In that case the stream is not executed.
 
Ruben Young On
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Ruben,

welcome to the Ranch and enjoy the stay!

A stream can be without a terminal operation. In that case the stream is not executed.



Thank you!

So in that case, is there a difference between a stream and a stream pipeline? Because the question asks about stream pipelines specifically. If they are the same, and the answer indicates it is required, it seems to me it cannot be without a terminal operation, which would make A false.

Edit: If there are no terminal operations, and a stream is not executed it does not run succesfully right? Which to me seems A does not apply to the question.
 
Bartender
Posts: 3955
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say there is a big difference between stream and stream pipeline ;)
Stream is an approach to access data stored in some backed storage (collection, file, generator)
Pipeline -- consists of a stream, zero or more intermediate operation, and exactly one terminal operation.
 
Ruben Young On
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mikalai Zaikin wrote:I would say there is a big difference between stream and stream pipeline ;)
Stream is an approach to access data stored in some backed storage (collection, file, generator)
Pipeline -- consists of a stream, zero or more intermediate operation, and exactly one terminal operation.



So then A would be incorrect right? Because having 0 terminal operations is not allowed.
 
Piet Souris
Bartender
Posts: 5584
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just read the API of a Stream (API Stream) and it says:

A stream pipeline consists of a source (which might be an array, a collection, a generator function, an I/O channel, etc), zero or more intermediate operations (which transform a stream into another stream, such as filter(Predicate)), and a terminal operation (which produces a result or side-effect, such as count() or forEach(Consumer)). Streams are lazy; computation on the source data is only performed when the terminal operation is initiated, and source elements are consumed only as needed.

So, a terminal operation is needed, apparently. But that raises the stupid question: is 1 AT MOST 1? I can imagine your doubts. Maybe Jeanne can shed her light on this.

I always thought that a terminal operation was not needed for a pipeline.
 
Mikalai Zaikin
Bartender
Posts: 3955
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's more English language area
"at most one" means "not more than one" (IMHO)
and "one" falls into this category
 
Ruben Young On
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mikalai Zaikin wrote:I think it's more English language area
"at most one" means "not more than one" (IMHO)
and "one" falls into this category



But 0 also falls under "at most one". At least when I hear "at most one" I think 0 or 1 if negative numbers are excluded. Therefore it's not really the same to me.
For me its similar to the idea of OptionalInt containing at most one integer value, which means 0 as OptionalInt.empty() or 1 with OptionalInt.of(4).

I guess I want to be very precise about the language used, because the review questions require you to look very precisely to spot mistakes.
 
author & internet detective
Posts: 42103
933
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
There is precision and there is reading into things .

We use stream and stream pipeline interchangeably in many places in the book. We are careful where it matters. For example the stream() method is not a pipeline.

Yes, "at most one" means "not more than one". Which means "optional". The terminal option isn't required.  However, the pipeline isn't useful with one because it returns a stream reference instead of executing.
 
Ruben Young On
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:There is precision and there is reading into things .

We use stream and stream pipeline interchangeably in many places in the book. We are careful where it matters. For example the stream() method is not a pipeline.

Yes, "at most one" means "not more than one". Which means "optional". The terminal option isn't required.  However, the pipeline isn't useful with one because it returns a stream reference instead of executing.



But the question specifically asks about streams that run succesfully. If there are no terminal operations, then the stream would not run succesfully, making A false right?
 
Jeanne Boyarsky
author & internet detective
Posts: 42103
933
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
No. A is still true. I think you might find it easier to understand why the opposite is false.

Inverting option A gives "A stream can have more than one terminal operations". That's clearly false. We both agree there can't be two.

 
Ruben Young On
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:No. A is still true. I think you might find it easier to understand why the opposite is false.

Inverting option A gives "A stream can have more than one terminal operations". That's clearly false. We both agree there can't be two.



I understand what you are saying, but I guess I find the language confusing still. Would you say the following is true?
   A stream pipeline can have one or more terminal operations.

Because in this case the opposite (less than one) is also clearly false.
 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that is incorrect.

A stream pipeline can have zero terminal operations without problems. It just doesn't do anything.
 
Ruben Young On
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:No, that is incorrect.

A stream pipeline can have zero terminal operations without problems. It just doesn't do anything.



As per the JavaDocs I am pretty sure it is a requirement:

A stream pipeline consists of a source (...), zero or more intermediate operations (...), and a terminal operation (which produces a result or side-effect, such as count() or forEach(Consumer)). This was mentioned earlier by Piet Souris.
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, the original question says:

Which are true statements about terminal operations in a stream that runs successfully?



I will admit I may have caused some confusion with my previous post. In that post, I said that a stream pipeline may have zero terminal operations. I made that assertion about some stream, not the specific case of "a stream that runs successfully", which was mentioned in the original problem statement. Here's a simple proof:
This piece of code creates a stream pipeline with one intermediate operation and zero terminal operations. It does not do anything useful, but the program compiles and runs just fine. However, I agree that with the added context of "a stream that runs successfully", you would need exactly one terminal operation.

However, that does NOT make the statement "At most one terminal operation can exist in a stream pipeline" false. That statement simply says that the number of terminal operations must be less than or equal to one. That statement does NOT imply that the statement "There must be exactly one terminal operation" can not be true at the same time. The ONLY thing it means is that you may never have more than one terminal operation, which is true for ALL streams.

Now, you try to flip the logic back on Jeanne by saying "A stream pipeline can have one or more terminal operations" by using the same logic that we used to state "At most one terminal operation can exist in a stream pipeline".

You are absolutely correct, if the question contained the answer possibility "A stream pipeline can have one or more terminal operations", then that would be a valid answer as well. That statement can be true at the same time as the statement "There must be exactly one terminal operation".
 
Ruben Young On
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
I will admit I may have caused some confusion with my previous post. In that post, I said that a stream pipeline may have zero terminal operations. I made that assertion about some stream, not the specific case of "a stream that runs successfully", which was mentioned in the original problem statement. Here's a simple proof:
This piece of code creates a stream pipeline with one intermediate operation and zero terminal operations. It does not do anything useful, but the program compiles and runs just fine.


As per the JavaDocs, a stream pipeline consists of a terminal operations. The code example would be a stream, but not a stream pipeline since it does not consist of a terminal operation. The difference here was also explained by Mikalai Zaikin earlier.

So when I hear exactly one, I think it should be nothing more and nothing less. Which is why I think the possibility of 0 terminal operations is false.
 
Jeanne Boyarsky
author & internet detective
Posts: 42103
933
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

Ruben Young On wrote:
As per the JavaDocs, a stream pipeline consists of a terminal operations.


Making a note of this for next time.
 
A tiny monkey bit me and I got tiny ads:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic