Peek() really to see the elements as they flow past a certain point in a pipeline

Vishwa Ratna :

My problem in most simple expressible way:

According to JavaDoc :

Peek() method exists mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline.

I have a pipe of 10 Meters and at the distance of 3 and 7 meter from input head i have two markers [aka peek()] for checking/debugging my elements.

Now from Input end i am giving input of 1,2,3,4,5.

At the point x = 4 meter , i have a filter() which filters all elements less than and equal to 3.

Now as per Java doc i should be able to see what has happened to my input in pipeline at distance 3 and 7 meters.

Output at marker1 at distance 3 (.peek()) should be 1,2,3,4,5 shouldn't be?? and output at marker2 at distance 7 should be 4,5 obviously.

But this is not happening in actual, the output is coming at 1st market(.peek()) just 1,2,3 and at 2nd it is coming 4,5.


The code that i executed to test my theory:

final List<Integer> IntList=
    Stream.of(1, 2, 3, 4, 5)
    .peek(it -> System.out.println("Before Filtering "+it)) // should print 1,2,3,4,5
    .filter(it -> it >= 3)
    .peek(it -> System.out.println("After Filtering: "+it)) //should print 4,5
    .collect(Collectors.toList());

Actual Output:

Before Filtering 1
Before Filtering 2
Before Filtering 3
After Filtering: 3
Before Filtering 4
After Filtering: 4
Before Filtering 5
After Filtering: 5

Expected Output (what a dev should think after reading JavaDoc (...exists mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline...)

    Before Filtering 1
    Before Filtering 2
    Before Filtering 3
    Before Filtering 4
    Before Filtering 5
    After Filtering: 4
    After Filtering: 5

If .peek() is not just for debugging at a particular point in pipeline then it the def is ambiguous.

Sorry for my story of Pipe , i thought this way i could explain my best what i want to ask.

Reinstate Monica - ζ-- :

No. Streams may be evaluated lazily as needed, and the order of operations is not strongly defined, especially when you're peek()ing. This allows the streams API to support very large streams without significant waste of time and memory, as well as allowing certain implementation simplifications. In particular, a single stage of the pipeline need not be fully evaluated before the next stage is.

Suppose how wasteful the following code would be, given your assumptions:

IntStream.range(1, 1000000).skip(5).limit(10).forEach(System::println);

The stream starts with one million elements and ends up with 10. If we evaluated each stage fully, our intermediate would be 1 million, 999995, and 10 elements, respectively.

As a second example, the following stream cannot be evaluated a stage at a time (because IntStream.generate returns an infinite stream):

IntStream.generate(/* some supplier */).limit(10).collect(Collectors.toList());

Your pipeline does indeed pass every single element through the first peek, and then only a subset through the second peek. However, the pipeline performs this evaluation in an element-major rather than stage-major order: it evaluates the pipe for 1, dropping it at the filter, then 2. Once it evaluates the pipe for 3, it passes the filter thus both peek statement execute, and the same then occurs for 4 and 5.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=84337&siteId=1