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!