If I have a Stream<T>
, I can easily use skip(long)
to skip the first few elements of a stream. However, there seems to be no equivalent for skipping a given number of elements at the end of the stream.
The most obvious solution is to use limit(originalLength - elementsToRemoveAtEnd)
, but that requires knowing the initial length beforehand, which isn't always the case.
Is there a way to remove the last few elements of a stream of unknown length without having to collect it into a Collection
, count the elements and stream it again?