Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

Stream.allMatch()

Checks whether all elements match a condition.

Available since: Java 8

Syntax

boolean allMatch(Predicate<? super T> predicate)

Parameters

ParameterTypeDescription
predicatePredicate<? 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
true

Notes

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

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.

Thanks for reading!

Donate to the site — we work hard for you.