Stream.allMatch()
Checks whether all elements match a condition.
Available since: Java 8
Syntax
boolean allMatch(Predicate<? super T> predicate)Parameters
| Parameter | Type | Description |
|---|---|---|
| predicate | Predicate<? super T> | The condition to check. |
Return value
boolean`true` if all elements match.
Examples
Validating input and an empty stream:
void main() {
List<String> fields = List.of("Anna", "anna@mail.com", "");
IO.println(fields.stream().allMatch(f -> !f.isBlank()));
IO.println(Stream.<String>empty().allMatch(f -> !f.isBlank()));
}Output:
false
trueNotes
It stops at the first non-matching element. For an empty stream it returns true — all zero elements match. Keep this in mind if an empty list should count as an error.
See also
- Stream.anyMatch() — Checks whether at least one element matches a condition.
- Stream.noneMatch() — Checks that no element matches a condition.
Related recipes
In the tutorial
- Stream operations — The main Stream API operations — filter, map, sorted, distinct, limit, skip, count, reduce, anyMatch, findFirst and numeric streams.