Streams do not “have” elements; they process them.
Also, Streams implement lazy execution; an element is only taken by the first Stream when the terminal operation requires it.
In that case, creating a second Stream in line 5.
Biniman Idugboe wrote:. . . . What process is the stream performing? . . .
It has produced a second Stream; in line 11 you are asking it to create a third Stream. Even though the first Stream<Long> has not processed any Longs, it is still marked as having been operated on, and your second request causes it to throw an exception.
In the following, how come the sourceStream has already been operated upon even when there is no terminal operation? . . .
Biniman Idugboe wrote:Is the term stream a mask of something far more complicated going on behind the scene?
Biniman Idugboe wrote:For example, where did ReferencePipeline.filter (ReferencePipeline.java:164) come from?
Yes. There are several package‑private classes in the java.util.stream package which implement the various interfaces, and that is one of them. I don't know ay more detaills; I don't think that I need to know anything.
Biniman Idugboe wrote:Is the term stream a mask of something far more complicated going on behind the scene? . . .
No, it isn't, but you should start with the simplest cases you can find and work through them until you understand them.
Biniman Idugboe wrote:. . . understanding of the concept of stream is for people with special capability.
So far, so good.
. . .
Don't say things like, “Java speak”. What you are doing is creating an object that will pass the elements of the array to something else, doing some manipulation of those elements as it does so. The word “process” has a specific meaning and you are not creating a process with that specific meaning.
2. I create a process line that hooks up to the data source and would allow data to enter the process line sequentially when the process starts. In Java speak, I think I create a stream from the data source:
They are different objects, the one created to manipulate a subset of the data taken by the first Stream. They both operate on different parts of the same data pipeline, yes.
. . , I add an intermediate operation to the stream: . . . What is different between IntPipeline$Head@2a2d45ba and IntPipeline$9@3c0f93f1? Is the pipeline not one continuous pipeline?
And as you said, nothing is going to happen until a terminal operation starts to execute.
. . . nothing happening yet. I decide to add a viewing station where I can view the data as it passes by. . . . There is nothing to view because the process has not started yet.
It might be better to say you are completing the pipeline. Just as whenever I am trying to lead water around the house in copper pipe
. . . I terminate the process line . . .
That sort of thing is an implementation detail. Maybe something else sends the elements into the pipeline. You have no way of knowing whether it is accurate or inaccurate, except possibly if you read the source code, which somebody else has told you about already.
(that tells IntPipeline$Head@2a2d45ba to start allowing data into the stream).
. . . While the above interpretation may very well be a punishable offence in Java land
No, you are adding lots of things you don't really need to know; JShell doesn't help by calling toString() on your Stream objects. I don't think you ever need to call toString() on a Stream. Everything you don't need to know is making things too complicated for you and causing you confusion.
. . . I am relieved of a lot of things complicated.
Don't try creating a parallel Stream from something with five elements, not unless the operations you are doing take a very long time (e.g. factorisation). If you want a parallel Stream. create one with millions fo elements to handle:-
I reckon I may have millions of data . . . parallel streams . . . a parallel stream from the original data source:
Don't know, but I think probably not. It is much more likely that 5 data will be sent to one Stream and a second Stream will be created handling nothing. Or two to one Stream and three ot the other. There is no way to find out, but it is only worth creating parallel Streams when there is a lot of work to be done. And those Streams are not called sequential.
. . . My computer has eight processors and the number of data to process is five . . . I am assuming that five sequential streams have been created. Is that correct?
No, it is a bit of confusing information JShell has given you. You can find out more about that class by exploring the source, or reflection, but both will overload you with useless information.
. . . java.util.stream.SortedOps$OfInt@3632be31. Is this also a pipeline?
It is much more complicated than that. Not only do you have an unknown number of parallel processes (and what is running on each core is called a process), but each is entering in to a race condition to print its result to System.out. Remember the println() method probably isn't thread‑safe; you might get the ints displayed out of order. As you said, you won't see the sorting order for parallel Streams until you collect their results all into a final data structure.
8. I terminate the stream with a forEach() operation: . . . Again, I am assuming that I ended up with five parallel pipelines as follows: . . . the final data printed to the console may not be sorted because the order will depend on the sequence in which the processors finished processing their pipelines. . . .
It might be better to say you are completing the pipeline. Just as whenever I am trying to lead water around the house in copper pipelines, I am not going to turn the water on at the source until I am confident I have connected the other end. When you build a Sahara Pipeline to carry oil across the desert, you don't turn the oil supply on until you are sure the other end is connected correctly.
JShell doesn't help by calling toString() on your Stream objects.
...but it is only worth creating parallel Streams when there is a lot of work to be done. And those Streams are not called sequential.
...it is a bit of confusing information JShell has given you.
...Start small and simple.
You have seen that your sorted parallel Stream doesn't complete the sorting until all the elements are collected together. You are calling the forEach() method at a stage too early.
Biniman Idugboe wrote:. . . Not sure I understand this. What is wrong with sortedStream.forEach(m -> System.out.println(m));? . . . . To hear now that jshell could give confusing information, I am lost.
Find the thread I split off about why people think Streams are so difficult. It was in the JavaRanch journal recently, maybe April.
Surely, I would love to, but I have not found a stream that is simple. All the streams I have encountered so far are derivatives of the same complicated Stream.
That's a pleasure
I appreciate your comments. They are very helpful to me.