CKTIS / Java
Stream.dropWhile: practical reference
Stream.dropWhile: practical reference: Stream.dropWhile(predicate) is a stateful intermediate operation that removes the longest matching prefix from an ordered stream.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
Stream.dropWhile(...)Stream.dropWhile(predicate) is a stateful intermediate operation that removes the longest matching prefix from an ordered stream.
Use it to skip a header or warm-up segment; after the first false, every later element remains even if it matches again.
On an unordered stream it may nondeterministically drop any subset of matching elements, so there is no logical prefix meaning.
It can be expensive in an ordered parallel pipeline; if all elements match the result is empty, and a null predicate causes NullPointerException.
The example exercises the documented element in a small deterministic program and compares exact standard output.
Examples
public class Main {
public static void main(String[] args) throws Exception {
var result = java.util.stream.Stream.of(1, 2, 0, 3).dropWhile(n -> n > 0).toList();
System.out.println(result);
}
}
Result
[0, 3]
The example exercises the documented element in a small deterministic program and compares exact standard output.