Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inclusive takeWhile() for Streams

I want to know if there is a way to add the last element of the stream that was tested against the condition of the method takeWhile(). I believe I want to achieve something similar to RxJava's takeUntil() method.

I'm guessing there is no direct way to do this (correct me if I am mistaken), but I would like to know if there is a proper workaround to achieve this that I am now aware of.

I've searched throughout Stack Overflow with little to no success. If you think there are threads that could solve my problems, I would certainly like to see it.

If you look at the following code's peek() you will see that the number 5 is checked against the takeWhile() condition but it never arrives in the forEach():

IntStream.of(1, 3, 2, 5, 4, 6)
        .peek(foo -> System.out.println("Peek: " + foo))
        .takeWhile(n -> n < 5)
        .forEach(bar -> System.out.println("forEach: " + bar));

The expected result is for the last element checked against takeWhile's condition to arrive at the forEach's System.out::println. In this case it is 5.

Thanks to everyone!

like image 209
MugenTwo Avatar asked Apr 01 '19 10:04

MugenTwo


People also ask

What is takewhile & dropWhile operations in streams?

What is takeWhile & dropWhile operations in Streams? takeWhile expects a predicate and returns a new stream consisting only of the elements that match the given predicate. But, it’s classified among three different cases. Let’s understand all of them,

What is stream takewhile () method in Java with examples?

Stream takeWhile() method in Java with examples. The takeWhile(java.util.function.Predicate) method returns a stream of the remaining elements of this stream after taken the longest prefix of elements that match the given predicate if the stream is ordered else a stream of a subset of elements taken from this stream that match the given predicate.

What is the use of takewhile in Java?

The takeWhile(java.util.function.Predicate) method returns a stream of the remaining elements of this stream after taken the longest prefix of elements that match the given predicate if the stream is ordered else a stream of a subset of elements taken from this stream that match the given predicate.

What happens when you use inclusive flag with takewhile ()?

// with inclusive flag, the value causing the predicate to return false will also be emitted 9 .pipe(takeWhile(val=>val <=3,true)) 10 // log: 1, 2, 3, 9


1 Answers

There's no convenient way to do that with the normal stream API. It is possible in an ugly way (you would need to adapt this implementation, that's just a normal takeWhile "backported" for Java 8).

This guy has written a stream extension library which has takeWhileInclusive.

Sample usage:

IntStreamEx.of(1, 3, 2, 5, 4, 6)
    .peek(foo -> System.out.println("Peek: " + foo))
    .takeWhileInclusive(n -> n < 5)
    .forEach(bar -> System.out.println("forEach: " + bar));
like image 134
Michael Avatar answered Oct 27 '22 23:10

Michael