This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!

Biniman Idugboe

Ranch Hand
+ Follow
since Jun 09, 2017
Cows and Likes
Cows
Total received
3
In last 30 days
0
Total given
0
Likes
Total received
7
Received in last 30 days
1
Total given
80
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Biniman Idugboe

Apology! When I say process line, I am referring to a factory process line, not a processor process.

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.


That makes it a whole lot easier to understand why a terminal operation is required before the pipeline can start to work.

JShell doesn't help by calling toString() on your Stream objects.


Not sure I understand this.  What is wrong with sortedStream.forEach(m -> System.out.println(m));?

...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.


I suppose parallelizing the stream logically partitions the stream into individual portions. Are you saying the individual portions are not ultimately processed sequentially?

...it is a bit of confusing information JShell has given you.


I have been going around with the notion that using Notepad or something similar is the simplest way to start learning to write Java codes. But, I need jshell to run code snippets. To hear now that jshell could give confusing information, I am lost.

...Start small and simple.


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.
I appreciate your comments. They are very helpful to me.
2 weeks ago
Well, it appears the understanding of the concept of stream is for people with special capability. I certainly do not belong in that group.  Nevertheless, I still have questions to ask about stream.
1.  I create a data source:

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:

3.  I add a data processing station to the line.  That is, 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?
4. So far, I just have a process line; nothing happening yet. I decide to add a viewing station where I can view the data as it passes by.

All I still have is just a process line.  There is nothing to view because the process has not started yet.
5.  I terminate the process line on a processing station that starts the process (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, please, forgive me. With it, I am relieved of a lot of things complicated.
I reckon I may have millions of data to process.  I want to have parallel streams (with my process line analogy, I am little bit more comfortable talking about stream).
6.  I create a parallel stream from the original data source:

My computer has eight processors and the number of data to process is five, that is three processors more than the number of data to process. I am assuming that five sequential streams have been created.  Is that correct?
7.  For the sake of it, I decided to add a sorted() operation to the stream:

I see sortedStream ==> java.util.stream.SortedOps$OfInt@3632be31.  Is this also a pipeline?
8. I terminate the stream with a forEach() operation:

Again, I am assuming that I ended up with five parallel pipelines as follows:
Processor1:  parallelStream.sorted().forEach(m -> System.out.println(m));
Processor2:  parallelStream.sorted().forEach(m -> System.out.println(m));
Processor3:  parallelStream.sorted().forEach(m -> System.out.println(m));
Processor4:  parallelStream.sorted().forEach(m -> System.out.println(m));
Processor5:  parallelStream.sorted().forEach(m -> System.out.println(m));
Each pipeline has a single data to process and the result of sorting a single data is the single data itself. Thus, 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.
Is that correct?
2 weeks ago
Is the term stream a mask of something far more complicated going on behind the scene?  For example, where did ReferencePipeline.filter (ReferencePipeline.java:164) come from?
2 weeks ago

Streams do not “have” elements; they process them.


Arrays.stream() produces a stream. What process is the stream  performing?

Also, Streams implement lazy execution; an element is only taken by the first Stream when the terminal operation requires it.


In the following, how come the sourceStream has already been operated upon even when there is no terminal operation?
2 weeks ago

The above pipeline does not have a terminal operation. The map() method has produced a stream named resultOfMapOperation.  Pleases, bear with me, I would like to know the elements that are in the resultOfMapOperation at this stage of the pipeline.
2 weeks ago
Again I am embarrassed to say that the concept of consumer passed to the tryAdvance() method is eluding me.

1.  The terminal operation is responsible for producing the ultimate result of the pipeline operations.
2.  The terminal operation does not produce a stream, does not produce a spliterator.
3.  The terminal operation creates a consumer, calls the tryAdvance() method of the spliterator created by the preceding operation and passes the consumer to it.
4.  The consumer performs action on the element brought forward by the tryAdvance() method.
In fact, I cannot continue to enumerate the steps because the concept starts to blur from this point forward. An intermediate operation is supposed to do something to the element, still the consumer does yet another thing to the element.  I just don't get it.
1 month ago
In my previous thread:

In the example code that you gave, nothing happens at all because there is no terminal operation. But let's assume that you added the terminal operation .forEachOrdered(System.out::println).

The forEachOrdered() method would call the tryAdvance() method of the map operation, which in turn calls the tryAdvance() method of the filter operation, which in turn calls the tryAdvance() method of the source. The source takes the integer 2 and passes it to the Consumer that the filter operation passed to it. That Consumer checks that 2 is greater than or equal to 3, determines that it's not, and so does nothing more. Control returns to the tryAdvance() method of the source stream, which just returns the value 'true' to indicate to the filter operation that there are more elements remaining, which the filter returns back up to the map operation, which the map operation returns to the forEachOrdered operation.

Because there are more elements remaining, the forEachOrdered operation calls the tryAdvance() method of the map operation a second time. This will call the tryAdvance() method of the filter operation. This will call the tryAdvance() method of the source. The source passes the integer 4 to the Consumer that was passed in by the filter operation. The Consumer checks that 4 is greater than or equal to 3, determines that it is, and so it passes 4 to the Consumer that was passed in by the map operation. The map's Consumer then applies the toString() method on the 4, and passes the String "4" to the Consumer that was passed in by the forEachOrdered operation. The forEachOrdered operation's Consumer then calls System.out.println() on "4".


The understanding I have of the above explanation is that an element is taken from the stream, the element passes through the pipeline until it reaches the terminal operation.  Then the next element is taken from the stream and the cycle repeats until all the elements have been processed.  Now, I have difficulty comprehending the consumer because I am struggling to reconcile the following:

I am thinking the filter() operation has already been completed; without waiting for the map() operation.

I am also thinking the map() operation has already been completed; without waiting for the next operation.

1 month ago

... the convention that in Java, when specifying a range, the starting index is inclusive and the ending index is exclusive.


The range is cut in half, NOT the data source.


Noted with thanks.

When trySplit() is called on the spliterator (by other code in the Stream API, not by you) and it succeeds, the original spliterator is modified so it will only be responsible for half of what it did before, and it will return a new spliterator that is responsible for iterating over the remaining elements.


The codes that perform the modification are not obvious from the ArrayList example above.
Now, I go to the tryAdvance(Consumer<? super E> action).  Could you give an example of the lamda expression that implements the consumer?
1 month ago

What will happen if I did the following:


1 month ago
Supose I have the following:

That gives me a spliterator that iterates from 0 to the end of the arraylist.

That gives me a spliterator that iterates from splitIndex to toIndex. Although toIndex is now set to splitIdex, the trySplit() method ends without executing the new ArraySpliterator<>().spliterator() again. So, how is the promise to only iterate from
0 to toIndex for the first half fulfilled?
Besides, since the secondHalf has already taken the elements from splitIndex to the end of the arraylist, should line 67 not be this.toIndex = splitIndex - 1?
Bear with me.  I'm trying to be sure I understand the concept.
1 month ago
apology for using image. I was just trying to visualize the concept.

Spliterators don't "contain" any elements at all, nor do they enclose a collection. They're just objects that have access to the internals of a collection that does contain the elements.


Right. The previous example did show that the array and ArraySpliterator are in the same implementation of ArrayList.

It promises to iterate only from splitIndex to toIndex.  Where is the promise to iterate only from 0 to splitIndex - 1? Are there automatic coding going on behind the scene?  
Are the promises actually iterating on the original spliterator or are they iterating on the underlying collection?
1 month ago
My questions are in the image insert below.
1 month ago
Please, have a look at the lines of codes below and the questions that follow.

The list remains intact even after splitting the spliterator.  So, I ask:  
Does the spliterator create a collection (repository) to which it copies the content of list and then operate on this spliterator-created collection?  
Does the spliterator also create separate collection in which to store the left hand portion of the split?
1 month ago