In: Computer Science
What is a terminal condition in stream? Provide an example.
The allMatch operation
The allMatch()operation answers the question: Do all elements in the stream meet this condition? It returns true if and only if all elements match a provided predicate, otherwise return false.
This is a short-circuiting terminal operation because the operation stops immediately if any unmatched element is found (just like short-circuit behavior of the AND operator).
The following example checks if all persons are male:
boolean areAllMale = listPersons.stream()
.allMatch(p -> p.getGender().equals(Gender.MALE));
System.out.println("Are all persons male? " + areAllMale);