Stream dropWhile() method in Java with examples
The dropWhile(java.util.function.Predicate) method returns two different types of stream depending upon whether the stream is ordered or not.
If the stream is ordered then a stream of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate is returned by method else a stream consisting of the remaining elements of this stream after dropping a subset of elements that match the given predicate.
In the case of the ordered stream, the longest prefix is a contiguous sequence of elements of this stream that match the predicate passed as a parameter to this method. The first element of the sequence is the first element of this stream, and the element immediately following the last element of the sequence does not match the given predicate.
In the case of the unordered stream, some elements of this stream match the given predicate and behaviour of this operation becomes nondeterministic; so this method is free to drop any subset of matching elements.
There may be some cases when all elements of this stream match the given predicate then this method will drop all elements and the result is an empty stream irrespective of order of Stream and when no elements of the stream match the given predicate then no elements are dropped and the result is the same as the input stream.
default Stream<T> dropWhile(Predicate<T> predicate)
Parameters: This method accepts a single parameter predicate which is a non-interfering, stateless predicate to apply to elements to determine the longest prefix of elements.
Return value: This method returns the new stream.
Below programs illustrate dropWhile(java.util.function.Predicate) method:
The output printed on console of IDE is shown below.
The output printed on console is shown below.