Biniman Idugboe

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

Recent posts by Biniman Idugboe

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 week 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 week 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 week ago

What will happen if I did the following:

1 week 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 week 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 week ago
My questions are in the image insert below.
1 week 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 week ago
I cannot thank you enough for tolerating me.
2 weeks ago
If I understood what you just said, the stream() method implements the Stream interface behind the scene and each abstract method is given codes that implement the method's own version of Spliterator<T>.  The resulting spliterator actually performs the operation expected of the method.  That certainly saves the programmer a lot of headache.  Right?
2 weeks ago
What I mean is, suppose I break the code down like the following:

Where was the variable named spliterator assigned an instance of Spliterator<T> ?
2 weeks ago
Still not clear to me.  In what order does the new StreamImpl<>(new MappingSpliterator(spliterator)); execute?
2 weeks ago
Good to know that I did not deviate far from the discussion.

The above line of code is kind of confusing to me because the variable named spliterator was not assigned any object up till this point.  Neither was the previousOperation assigned any object.  Is it the case that a null is accepted as a spliterator?
2 weeks ago
Permission to divert this discussion a little bit.  I have been of the understanding that when an interface is implemented, all the abstract methods in the interface will be given codes in the process of implementation.

The Stream interface has several abstract methods.  No codes were added to the methods.  Does implementing an interface simply mean incorporating the interface into a class definition?
2 weeks ago
Suppose I have:

Does it mean that StreamImpl{} will enclose the five operations and each of the non-terminal operations will in turn implement its own spliterator?  As in the following?
The source_spliterator will be passed to the filter() method
The filter_spliterator will be passed to the map() method
The map_spliterator will be passed to the limit() method
The limit_spliterator will be passed to the max() method

Suppose also that in the previous case where I have list,stream().map().forEach(), I do the following:

To set the ball rolling, I would need to do something like:

What will repeatedly invoke x_Spliterator.tryAdvance() so that all the elements in x_spliterator can be processed?
2 weeks ago